Track the First Unique Restaurant in a Stream

Quick Overview

Track integer restaurant IDs in arrival order and answer each query with the earliest value seen exactly once so far, or `-1` when no unique value remains.

Track the First Unique Restaurant in a Stream

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Restaurant IDs arrive one at a time. Support two operations: - `add(value)`: append one occurrence. - `showFirstUnique()`: return the earliest-arriving value that has appeared exactly once so far, or `-1` if no such value exists. Process an operation sequence and return the result of every `show` operation. ### Function Contract Implement `firstUniqueStream(operations)`, where operations are `['add', value]` or `['show']`. Return an integer array. ### Constraints & Assumptions - `0 <= len(operations) <= 200,000`. - Values are signed 64-bit integers; `-1` is reserved for the no-result output and will not be added. - Once a value appears a second time, later occurrences can never make it unique again. - Operations are processed in the supplied order. ### Clarifying Questions to Ask - Does “first” mean smallest value? No, earliest arrival among currently unique values. - Can a value become unique again? No; counts only increase. - What is returned before anything is added? `-1`. - Must `show` mutate the state? No. ```hint Keep only current candidates in arrival order Use a count map plus an ordered structure containing values whose count is exactly one. Remove a value when its count reaches two. ``` ```hint Lazy deletion is also valid A queue may keep stale repeated values if each `show` removes stale elements from its front using the count map. ``` ### Example ```text operations = [ ["add", 4], ["add", 7], ["show"], ["add", 4], ["show"], ["add", 7], ["show"] ] ``` Return `[4, 7, -1]`. ### Evaluation Focus - Preserves arrival order among values that remain unique. - Removes or skips a value exactly when its second occurrence arrives. - Supports repeated `show` calls without changing the answer. - Runs in amortized `O(1)` time per operation with `O(u)` state for `u` distinct values. ### Extensions to Discuss 1. How could old values be evicted in a fixed-time window? 2. What memory remains necessary to distinguish a second from a third occurrence? 3. How would concurrent producers change ordering and synchronization?

Quick Answer: Track integer restaurant IDs in arrival order and answer each query with the earliest value seen exactly once so far, or `-1` when no unique value remains.

|Home/Coding & Algorithms/DoorDash
DoorDash logo
DoorDash
May 26, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Problem

Restaurant IDs arrive one at a time. Support two operations:

  • add(value) : append one occurrence.
  • showFirstUnique() : return the earliest-arriving value that has appeared exactly once so far, or -1 if no such value exists.

Process an operation sequence and return the result of every show operation.

Function Contract

Implement firstUniqueStream(operations), where operations are ['add', value] or ['show']. Return an integer array.

Constraints & Assumptions

  • 0 <= len(operations) <= 200,000 .
  • Values are signed 64-bit integers; -1 is reserved for the no-result output and will not be added.
  • Once a value appears a second time, later occurrences can never make it unique again.
  • Operations are processed in the supplied order.

Clarifying Questions to Ask Guidance

  • Does “first” mean smallest value? No, earliest arrival among currently unique values.
  • Can a value become unique again? No; counts only increase.
  • What is returned before anything is added? -1 .
  • Must show mutate the state? No.

Example

operations = [
  ["add", 4], ["add", 7], ["show"],
  ["add", 4], ["show"], ["add", 7], ["show"]
]

Return [4, 7, -1].

Evaluation Focus

  • Preserves arrival order among values that remain unique.
  • Removes or skips a value exactly when its second occurrence arrives.
  • Supports repeated show calls without changing the answer.
  • Runs in amortized O(1) time per operation with O(u) state for u distinct values.

Extensions to Discuss

  1. How could old values be evicted in a fixed-time window?
  2. What memory remains necessary to distinguish a second from a third occurrence?
  3. How would concurrent producers change ordering and synchronization?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...