Bloomberg SDE new Grad Rejected 1st Round
Anonymous User
2266

I was on a phone call with the recruiter and he was able to see what I could type on the given hackerrank link.(The call quality was really bad)
(Basic conversation)
The question comes up on the page when I had 20 min left in the call


Question (paraphrased) : Given the time operation hours of different banks, you have to test whether a transaction is valid or not. For a transaction to be valid it has to fall between the operating hours of the bank.

Input example - operation hours of different banks

04:20 - 12:30
19:00 - 21:45
14:00 - 17:30
12:30 - 14:00

Test - 11:00 - 17:30
Output - true


On the editor side, there was the main method and that's it. No function to scan the input or no class defined to store the time.


I - Can I expect all the operating hours to be in the same time zone.
Interviewer - Good Question. For now yes but what you would do if they were not?
I - Will convert them to one time zone and also the input to be tested to the same time zone. Also, does a transaction have to be completed from the same bank if it started in the same bank?
Interviewer - What do you mean?
I - Explained that for 2 banks with operating hours like
04:20 - 12:30
12:00 - 15:00
can handle one transaction which is like 11:00 - 14:00
Interviewer - Yes, How would you approach it.
I - Merge the time zones which can be merged. If the closing time of 1 bank is past the opening time of another, they both can be merged to set as one entry.
(some conversation on how it will take a lot of time and coming to the conclusion that I will sort the time zones based on opening time).
If they are sorted I can check each of them against one another and some gibberish and merge them.
(here I got a little messed up on how to optimize the merging process)
Interviewer - Once they are merged how would you check if the given transaction is valid or not.
I - For a transaction to be valid if its start time >= opening hour at an entry && end time <= closing hour at an entry, it would be valid.
Interviewer - Ok now code it.
I - You have to define some class or use some predefined to store the operating hours. Store it in an ArrayList. Sort it and merge and check.

I started writing code for an Object type I have no clue how it looks like and made a fairly messy solution.

In the end, the interviewer seemed satisfied but I got a rejection email the next day.


The thing I missed was that after sorting only adjacent entries have to be checked for merging.
Below is the code I wrote post-interview -

'''

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

class TimeSet implements Comparable<TimeSet> {
	Time start;
	Time end;

	public TimeSet(String st) throws NumberFormatException, Exception
	{
		int posSemiColon1 = st.indexOf(":");
		int posSemiColon2 = st.indexOf(":",posSemiColon1+1);

		this.start = new Time(Integer.parseInt(st.substring(0, posSemiColon1)), Integer.parseInt(st.substring(posSemiColon1+1, posSemiColon1+3)));
		this.end = new Time(Integer.parseInt(st.substring(posSemiColon2-2, posSemiColon2)), Integer.parseInt(st.substring(posSemiColon2+1, posSemiColon2+3)) );

		if (start.hour > end.hour)
			throw new Exception("End time have to be after start time");
		else if (start.hour == end.hour && start.min > end.min)
			throw new Exception("End time have to be after start time");
	}

	class Time {
		int hour;
		int min;

		public Time(int hour, int min) throws Exception {
			super();
			this.hour = hour;
			this.min = min;
			if (hour < 0 || hour > 23)
				throw new Exception("Hour out of Range");
			if (min < 0 || min > 59)
				throw new Exception("Min out of Range");
		}
	}

	@Override
	public int compareTo(TimeSet o) {
		// TODO Auto-generated method stub
		if (this.start.hour != o.start.hour)
			return this.start.hour - o.start.hour;
		return this.start.min - o.start.min;
	}

	@Override
	public String toString() {
		return start.hour + ":" + start.min + " - " + end.hour + ":" + end.min;
	}
}

public class Solver {

	public static void timeMerger(List<TimeSet> timeHolder) throws Exception {
		for (int i = 0; i < timeHolder.size() - 1; i++) {
			TimeSet present = timeHolder.get(i);
			TimeSet next = timeHolder.get(i + 1);

			if (present.end.hour > next.start.hour
					|| (present.end.hour == next.start.hour && present.end.min >= next.start.min)) {
				timeHolder.remove(i + 1);
				timeHolder.remove(i);
				TimeSet merged = new TimeSet(present.start.hour, present.start.min, next.end.hour, next.end.min);
				timeHolder.add(i, merged);
				i--;
			}
		}
	}

	public static TimeSet ScanningInput(List<TimeSet> timeHolder) throws Exception {
		Scanner sc = new Scanner(System.in);

		int howMany = Integer.parseInt(sc.nextLine());
		while (howMany > 0) {

			TimeSet t = new TimeSet(sc.nextLine());
			timeHolder.add(t);
			howMany--;
		}

		TimeSet toBeTested = new TimeSet(sc.nextLine());
		return toBeTested;
	}

	public static boolean isTransactionValid(TimeSet toBeTested,List<TimeSet> timeHolder) {

		for (int i = 0; i < timeHolder.size(); i++) {
			TimeSet present = timeHolder.get(i);
			if( (present.start.hour<toBeTested.start.hour || (present.start.hour == toBeTested.start.hour && present.start.min <= toBeTested.start.min)) 
					&& (present.end.hour>toBeTested.end.hour || (present.end.hour == toBeTested.end.hour && present.end.min >= toBeTested.end.min)) )
				return true;
		}
		return false;
	}

	public static void main(String[] args) throws Exception {

		List<TimeSet> timeHolder = new ArrayList<>();
		TimeSet toBeTested = ScanningInput(timeHolder);

		System.out.println("TimeSet Collection in the order timesets have been added");
		System.out.println(timeHolder);

		//sorting it 
		Collections.sort(timeHolder);
		System.out.println("TimeSet Collection After Sorting based on start time");
		System.out.println(timeHolder);

		//merging it 
		timeMerger(timeHolder);
		System.out.println("TimeSet Collection After Merging");
		System.out.println(timeHolder);

		System.out.println(isTransactionValid(toBeTested,timeHolder));
	}
}

/*
 Time complexity O(n(log(n))) 
 Sorting takes O(n(log(n)). All other operations are done in O(n) time.


 Input format 
 1st line defines number of time stamp available for banks to be operated 
 2nd to 2nd last lines contain all the time stamp availablity of banks
 Last line is the test to be checked.


Test case - For 4 entries of time stamp , the last one to be tested

4
04:20 - 12:30
19:00 - 21:45
14:00 - 17:30
12:30 - 14:00
16:00 - 17:30

 */

'''

Comments (1)