PracHub
QuestionsLearningGuidesInterview Prep
|Home/Coding & Algorithms/Databricks

Build a Constant-Time Snapshot Set Iterator

Last updated: Jul 28, 2026

Quick Overview

Design a mutable integer set whose iterators preserve insertion-order snapshots without copying the live set when they are created. Meet constant-time creation and update targets, support independent iterators and remove-then-readd behavior, and explain skipped history and safe reclamation.

  • hard
  • Databricks
  • Coding & Algorithms
  • Software Engineer

Build a Constant-Time Snapshot Set Iterator

Company: Databricks

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Onsite

# Build a Constant-Time Snapshot Set Iterator Design `SnapshotSet`, a set of integers whose iterators observe an immutable logical snapshot without copying the set at iterator creation. ```text add(value) -> bool remove(value) -> bool contains(value) -> bool getIterator() -> SnapshotIterator SnapshotIterator.hasNext() -> bool SnapshotIterator.next() -> int ``` `add` returns whether the value was newly present. `remove` returns whether a present value was removed. An iterator must yield exactly the values that were present when it was created, in insertion order. Later mutations must not alter that iterator, and multiple iterators must advance independently. If a removed value is later added again, treat it as a new insertion at the end. Each successful add therefore creates one historical insertion record. ## Constraints - `add`, `remove`, and `contains` must be amortized `O(1)`. - `getIterator` must be `O(1)` and may not copy current values. - With `N` historical insertion records and `M` live iterators, total structural space must be `O(N + M)`. - `next` may throw a standard exhausted-iterator error when no value remains. ## Example After `add(4)`, `add(9)`, create iterator `a`; then `remove(4)`, `add(7)`, create iterator `b`. Iterator `a` yields `4, 9`; iterator `b` yields `9, 7`. ## Clarifications Define the version or lifetime invariant that lets an iterator decide whether a historical insertion was active at its snapshot. Explain iterator cost, including skipped records. ## Hints Consider recording when an insertion becomes visible and when it stops being visible, while letting each iterator remember only a snapshot version and a cursor. ## Extensions - How would you reclaim history once old iterators are closed? - What thread-safety guarantees would you offer? - How would fail-fast iteration differ from snapshot iteration?

Quick Answer: Design a mutable integer set whose iterators preserve insertion-order snapshots without copying the live set when they are created. Meet constant-time creation and update targets, support independent iterators and remove-then-readd behavior, and explain skipped history and safe reclamation.

Related Interview Questions

  • Implement Generalized Tic-Tac-Toe - Databricks (hard)
  • IPv4 CIDR Range Membership Queries - Databricks (medium)
  • Design an n x n Tic-Tac-Toe Game - Databricks (medium)
  • Optimal Commute: Nearest Transit Distance in a City Grid - Databricks (medium)
|Home/Coding & Algorithms/Databricks

Build a Constant-Time Snapshot Set Iterator

Databricks logo
Databricks
Jul 7, 2026, 12:00 AM
hardSoftware EngineerOnsiteCoding & Algorithms
0
0

Build a Constant-Time Snapshot Set Iterator

Design SnapshotSet, a set of integers whose iterators observe an immutable logical snapshot without copying the set at iterator creation.

add(value) -> bool
remove(value) -> bool
contains(value) -> bool
getIterator() -> SnapshotIterator

SnapshotIterator.hasNext() -> bool
SnapshotIterator.next() -> int

add returns whether the value was newly present. remove returns whether a present value was removed. An iterator must yield exactly the values that were present when it was created, in insertion order. Later mutations must not alter that iterator, and multiple iterators must advance independently.

If a removed value is later added again, treat it as a new insertion at the end. Each successful add therefore creates one historical insertion record.

Constraints

  • add , remove , and contains must be amortized O(1) .
  • getIterator must be O(1) and may not copy current values.
  • With N historical insertion records and M live iterators, total structural space must be O(N + M) .
  • next may throw a standard exhausted-iterator error when no value remains.

Example

After add(4), add(9), create iterator a; then remove(4), add(7), create iterator b. Iterator a yields 4, 9; iterator b yields 9, 7.

Clarifications

Define the version or lifetime invariant that lets an iterator decide whether a historical insertion was active at its snapshot. Explain iterator cost, including skipped records.

Hints

Consider recording when an insertion becomes visible and when it stops being visible, while letting each iterator remember only a snapshot version and a cursor.

Extensions

  • How would you reclaim history once old iterators are closed?
  • What thread-safety guarantees would you offer?
  • How would fail-fast iteration differ from snapshot iteration?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Databricks•More Software Engineer•Databricks Software Engineer•Databricks Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 8,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • AI Coding Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.