Free beta: free to use with daily limits while we build. Join the waitlist for founding-member pricing at launch. Join the waitlist

Sign up free

Databricks interview question

Collection whose iterators see a snapshot of the contents at creation time

Company:
Databricks
Round:
Phone screen
Type:
Coding
Difficulty:
Medium
Reported:
Q1 2026

Problem

Implement a collection of values with three operations:

  • add(value): insert a value. Duplicates are allowed, so the collection behaves like a multiset.
  • remove(value): remove one occurrence of value.
  • iterator(): return an iterator over the values that were in the collection at the moment iterator() was called.

Later calls to add and remove must not change what an existing iterator produces, even if that iterator has already been partly consumed. Several iterators can be alive at the same time, each with its own snapshot. The order in which an iterator yields values does not matter.

What remove should do when the value is not present (ignore the call, report it or raise an error) is not specified; any documented choice is accepted.

Copying the whole collection inside iterator() is a fine first version; then make iterator creation cheap.

Example 1

Input: add(1), add(2), add(2), add(3), it = iterator(), remove(2), add(4), remove(1), list(it), list(iterator())
Output: [1, 2, 2, 3] and [2, 3, 4] (in any order)

it keeps the four values that existed when it was created, while a new iterator sees the current contents.

Constraints

  • The type of the values is not specified; a solution that indexes them in a hash map should state that it assumes hashable values.
  • A copy in iterator() costs O(n) per call; the improved version should create iterators without copying.

Reported by candidates or drawn from publicly available sources, and rewritten in our own words. Pathwise Prep does not publish confidential assessment content. Report a concern