Google Coding Round

540
Mar 20, 2025
Mar 20, 2025

//Updated with R3
R2. There is a train chart given, can I set out from source at given time and reach the destination before deadline.

Train   Departure City  Departure Time  Arrival City    Arrival Time
0       BOS             10:00           NYC             14:00
1       NYC             13:00           DC              15:00
2       NYC             14:30           DC              16:30
3       DC              18:00           ATL             21:00

Return the List of Trains taken from source to destination.

ReturnType computeJourney(Train[] trains, String source, String destination, String startTime, String deadline).

R1. When we upload an application on play store, we define an optional minSDKVersion and/or maxSDKVersion. We have to create an return the bucket of the version range (inclusive). (Don't remember the theory exactly but it was something like this)

App minSDKVersion   maxSDKVersion
A   4               10               
B   7               16
C   -               9
D   8               -

Output - [(<=3), (4-6), (7-7), (8-8), (9-9), (10-15), (16)]
We have to take appropriate input and output time. If there is missing minSDKVersion, there would be a range from 0 to LowestVersion but if there is a missing maxSDKVersion, there won't be any open bucket but the maximum SDK Version in the last bucket. Like (16) not (>=17)

R3.
/** A finite stream of Integers. */

interface Stream {
  
  /** Returns the next element from the Stream, or null if empty. */
  Integer getNext();
}

Given a functional interface which fetches next element of a stream or null. We need to return the first element which doesn't repeat.

public class FirstSingleElementStream {
  
  public Optional<Integer> getFirstSingleElement (Stream stream) {
    Map<Integer, Integer> elementFreq = new LinkedHashMap<>();
    
    while (true) {
      Integer element = stream.getNext();
      if (element == null) {
        break;
      }
// to handle edge case if freq goes beyound integer max value
      if (elementFreq.containsKey(element) && elementFreq.get(element) > 2) {
        continue;
      } else {
        elementFreq.put(element, elementFreq.getOrDefault(element, 0) + 1);
      }
    }
    
    for (Integer element : elementFreq.keySet()) {
      if (elementFreq.get(element) == 1) {
        return Optional.of(element);
      }
    }
    return Optional.empty();
  }
  
//follow up question if there is no LinkedHashMap, solved it using ArrayList and Map, map holds element and index in list and list holds the freq
  public Optional<Integer> getFirstSingleElementNoLinkedHashMap(Stream stream) {
    
  }
}
Comments (7)