Coding Test Answer

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Coding Test

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.

Language you are using: Java


Runtime Complexity of your solution: O(n/2)
Space Complexity of your solution: O(n)
Write your code below (continue on back if needed):
import java.util.Scanner;

class Palindrome {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);

String s = sc.nextLine();

System.out.println(isPalindrome(s));

public static boolean isPalindrome(String 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.

Language you are using: Java


Runtime Complexity of your solution: O(n)
Space Complexity of your solution: O(n)
Write your code below (continue on back if needed):
public static List<Integer> findFraudulent Transactions (List<Transaction> transactions) {

List<Integer> fraudulent Transactions = new ArrayList<>();

Map<Integer, List<Transaction>> transactionsByCard = new HashMap<>();

for (Transaction transaction: transactions) {

int transld = transaction.transld;


int cardid = transaction.cardId;

double amount = transaction.amount;

String city = transaction.city;

int time = transaction.time;

// Check for amount-based fraud

if (amount >= 10000) {

fraudulent Transactions.add(transld); }

// Check for city-time based fraud

if (transactionsByCard.containsKey(cardId)) {

for (Transaction prev Transaction: transactionsByCard.get(cardId)) {

String prevCity = prev Transaction.city;

int prevTime = prev Transaction.time;

if (!city.equals(prevCity) && Math.abs(time - prevTime) <= 30) { fraudulent Transactions.add(transld);

break;
}}

// Add the current transaction to the list for its credit card transactionsByCard.putlfAbsent(cardid, new
ArrayList<>());
transactionsByCard.get(cardid).add(transaction);

return fraudulent Transactions;

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.

Language you are using: Java


Runtime Complexity of your Solution: O(1)
Space Complexity of your Solution: O(n)
Write your code below (continue on back if needed):
import java.util.HashMap; import java.util.Map;

class UndergroundSystem {

// HashMap to store check-in information private Map<Integer, Checkininfo> checkinMap;

// HashMap to store travel times between station pairs private Map<String, TripInfo> tripMap;

public UndergroundSystem() {

checkinMap = new HashMap<>(); tripMap = new HashMap<>();

public void checkin(int id, String stationName, int t) { checkInMap.put(id, new


CheckinInfo(stationName, t));

public void checkOut(int id, String stationName, int t) {

Checkininfo checkininfo = checkinMap.get(id);

if (checkinInfo != null) {

String startStation = checkinInfo.stationName;


int checkinTime = checkinInfo.time;

String tripkey = startStation + "->" + stationName;

int travelTime = t checkin Time;

Tripinfo tripInfo = tripMap.getOrDefault(tripKey, new TripInfo());

tripInfo.totalTime += travelTime;

tripInfo.tripCount++;

tripMap.put(tripKey, tripInfo);

checkInMap.remove(id); // Remove the customer from check-in map as they've checked out

public double getAverage Time(String startStation, String endStation) {

String tripKey = startStation + "->" + endStation; TripInfo tripInfo = tripMap.get(tripKey);

return (double) tripInfo.totalTime/tripInfo.tripCount;

private static class CheckinInfo {

String stationName;
int time;

Checkininfo(String stationName, int time) {

this.stationName = stationName;

this.time = time;

private static class TripInfo { int totalTime; int tripCount;

TripInfo() {

this.totalTime = 0; this.tripCount = 0; }

}
public static void main(String[] args) {

UndergroundSystem undergroundSystem = new UndergroundSystem(); undergroundSystem.checkIn(45,


"Leyton", 3);

undergroundSystem.checkIn(32, "Paradise", 8);

undergroundSystem.checkIn(27, "Leyton", 10);

undergroundSystem.checkOut(45, "Waterloo", 15); // Customer 45, "Leyton" -> "Waterloo" in 12


minutes

undergroundSystem.checkOut(27, "Waterloo", 20); // Customer 27, "Leyton" -> "Waterloo" in 10


minutes
undergroundSystem.checkOut(32, "Cambridge", 22); // Customer 32, "Paradise" -> "Cambridge" in 14
minutes

System.out.println(underground System.getAverage Time("Paradise", "Cambridge")); // return


14.00000

System.out.println(undergroundSystem.getAverageTime("Leyton", "Waterloo")); // return 11.00000

undergroundSystem.checkIn (10, "Leyton", 24);

System.out.println(undergroundSystem.getAverageTime("Leyton", "Waterloo")); // return 11.00000


undergroundSystem.checkOut(10, "Waterloo", 38); // Customer 10, "Leyton" -> "Waterloo" in 14
minutes

System.out.println(underground System.getAverage Time("Leyton", "Waterloo")); // return 12.00000

You might also like