Coding Test Answer
Coding Test Answer
Coding Test Answer
Duration: 30 mins
Name:Prince Keshri
Position: Software Developer
Result:
This test is designed to test your coding fluency and problem-solving skills. Solutions should focus on solving
the given problem, producing clean, working code, and producing solutions which optimize time and space
complexity. You can use any programming language and you will not be penalized for minor syntax errors.
Problem 1: Given a string, write a function which returns a boolean value indicating if it is palindrome or not. A
string is said to be a palindrome if the reverse of the string is the same as the string. For example, malayalam is
a palindrome, geek is not.
class Palindrome {
String s = sc.nextLine();
System.out.println(isPalindrome(s));
int i=0;
s=s.toLowerCase();
int j=s.length()-1;
while(i<=j){
if(s.charAt(i)==s.charAt(j)){
i++;
else{
break;
if(j<i)return true;
return false;
}
Problem 2: Write a function which takes in a 2D List/Array of transactions and returns a list of transaction IDs
which are fraudulent. Any transaction greater than or equal to 10000 is considered fraudulent. Any transaction
from the same credit card in a different city within 30 minutes is considered fraudulent.
Input: A 2D List/Array of transactions with each transaction record having a transaction ID (integer), credit card
ID (integer, transaction amount (double), city (string), and time in minutes (integer). You can assume all
transactions happen on the same day.
Example:
Input: [ [1, 1000, 500.00, “Vadodara”, 0], [2, 1000, 500.00, “Mumbai”, 5], [3, 1001, 500.00, “Mumbai”, 10], [4,
1001, 10000.00, “Mumbai”, 10]]
Output: [2, 4]
Transactions 2 and 4 should be considered fraudulent. Transaction 2 occurred within 30 min of transaction 1
with the same credit card ID (1000) and in a different city. Transaction 4 has an amount is greater than or equal
to 10000.
fraudulent Transactions.add(transld); }
if (transactionsByCard.containsKey(cardId)) {
break;
}}
// Add the current transaction to the list for its credit card transactionsByCard.putlfAbsent(cardid, new
ArrayList<>());
transactionsByCard.get(cardid).add(transaction);
Problem 3: A railway system is keeping track of customer travel times between different stations. They are
using this data to calculate the average time it takes to travel from one station to another.
Implement a class called UndergroundSystem with the following functions:
void checkIn(int id, string stationName, int t)
A customer with a card ID equal to id, checks in at the station stationName at time t. A customer can only be
checked into one place at a time.
void checkOut(int id, string stationName, int t)
A customer with a card ID equal to id, checks out from the station stationName at time t.
double getAverageTime(string startStation, string endStation)
Returns the average time it takes to travel from startStation to endStation.
The average time is computed from all the previous traveling times from startStation to endStation that
happened directly, meaning a check in at startStation followed by a check out from endStation. The time it takes
to travel from startStation to endStation may be different from the time it takes to travel from endStation to
startStation. There will be at least one customer that has traveled from startStation to endStation before
getAverageTime is called. You may assume all calls to the checkIn and checkOut methods are consistent. If a
customer checks in at time t1 then checks out at time t2, then t1 < t2. All events happen in chronological order.
class UndergroundSystem {
// HashMap to store travel times between station pairs private Map<String, TripInfo> tripMap;
public UndergroundSystem() {
if (checkinInfo != null) {
tripInfo.totalTime += travelTime;
tripInfo.tripCount++;
tripMap.put(tripKey, tripInfo);
checkInMap.remove(id); // Remove the customer from check-in map as they've checked out
String stationName;
int time;
this.stationName = stationName;
this.time = time;
TripInfo() {
this.totalTime = 0; this.tripCount = 0; }
}
public static void main(String[] args) {