Problem · Hash Table

First Unique ID in a Stream

Learn this problem
MediumDoorDash logoDoorDashFULLTIMEONSITE INTERVIEW

Problem statement

Process a finite stream of operations while maintaining the earliest ID whose total occurrence count is exactly one.

  • [1, id] adds id to the stream.
  • [2] queries the earliest currently unique ID.
  • Return one string per query: the decimal ID, or the literal string null when no ID is unique.
  • An ID leaves the unique order on its second occurrence and never rejoins after later occurrences.

Function

firstUniqueResults(operations: int[][]) → String[]

Examples

Example 1

operations = [[1,2],[1,3],[2],[1,2],[2],[1,3],[2]]return = ["2","3","null"]

ID 2 is initially first. Its second occurrence removes it, leaving 3. After 3 repeats, no unique ID remains.

Example 2

operations = [[2],[1,-5],[2],[1,-5],[2]]return = ["null","-5","null"]

The first query is empty, then -5 is unique until its second occurrence.

Constraints

  • operations is non-empty.
  • Every operation is exactly [1, id] or [2].
  • IDs are signed integers.

More DoorDash problems

drafts saved locally
public String[] firstUniqueResults(int[][] operations) {
    // TODO: return the earliest currently unique ID for each query.
}
operations[[1,2],[1,3],[2],[1,2],[2],[1,3],[2]]
expected["2", "3", "null"]
checking account