Design a sparse set with O(1) insert, remove, contains and clear and O(k) iteration

Quick Overview

Design a sparse set over integers below a fixed capacity that supports insert, remove, membership and clear in constant time and lists its members in a required dense order. It tests array-based data structure design, invariants that hold up against stale memory, and careful complexity reasoning.

Design a sparse set with O(1) insert, remove, contains and clear and O(k) iteration

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Other

Design a sparse set that stores integers `x` with `0 <= x < capacity`. It must use `O(capacity)` space. Insertion, removal, membership tests and clear each run in `O(1)` time, and emitting the `k` current values takes `O(k)` time. `insert` and `remove` return `true` only when membership changed. Inserting a value that is already present, or removing one that is missing, returns `false` and changes nothing. `clear` empties the set and returns `null`. `iterate` returns the current dense sequence. A new value is appended to the end. Removing a present value moves the last current value into the removed value's slot. This exercise requires the stated dense sequence order. Results must not be sorted. Implement a class `SparseSet(capacity)` with the following methods: - `insert(x: int) -> bool`: Add `x` to the end of the dense sequence if it is not already present. Return `true` if it was added, `false` if it was already present. - `remove(x: int) -> bool`: If `x` is present, move the last value in the dense sequence into `x`'s slot and shrink the sequence by one, then return `true`. Return `false` if `x` is not present. - `contains(x: int) -> bool`: Return `true` if `x` is currently in the set. - `clear()`: Remove all values in `O(1)` time. - `iterate() -> List[int]`: Return the current values in dense-sequence order, in `O(k)` time for `k` values. Example 1: ```text Input: ["SparseSet", "insert", "insert", "insert", "insert", "contains", "remove", "iterate", "remove", "contains"] [[10], [3], [5], [7], [5], [5], [3], [], [3], [3]] Output: [null, true, true, true, false, true, true, [7, 5], false, false] ss = SparseSet(10) # capacity = 10, dense: [] ss.insert(3) # true dense: [3] ss.insert(5) # true dense: [3, 5] ss.insert(7) # true dense: [3, 5, 7] ss.insert(5) # false duplicate, unchanged ss.contains(5) # true ss.remove(3) # true last value 7 fills slot 0, dense: [7, 5] ss.iterate() # [7, 5] ss.remove(3) # false 3 is missing, unchanged ss.contains(3) # false ``` Example 2: ```text Input: ["SparseSet", "iterate", "insert", "insert", "insert", "remove", "iterate", "clear", "iterate", "contains", "insert", "iterate", "contains"] [[5], [], [4], [0], [2], [2], [], [], [], [4], [0], [], [4]] Output: [null, [], true, true, true, true, [4, 0], null, [], false, true, [0], false] ss = SparseSet(5) # capacity = 5, dense: [] ss.iterate() # [] empty set ss.insert(4) # true dense: [4] ss.insert(0) # true dense: [4, 0] ss.insert(2) # true dense: [4, 0, 2] ss.remove(2) # true 2 was the last element, dense: [4, 0] ss.iterate() # [4, 0] ss.clear() # null dense: [] ss.iterate() # [] ss.contains(4) # false stale data is ignored ss.insert(0) # true dense: [0] ss.iterate() # [0] ss.contains(4) # false ``` ```hint Packing the members To return `k` values in `O(k)` time, the members have to sit next to each other somewhere. Ask what you would need to find a value's position in that packed list in constant time. ``` ```hint Clearing without a loop A constant-time clear leaves old data in memory. Design the membership test so that stale entries cannot pass it. ``` ### Constraints & Assumptions - `1 <= capacity <= 10^5` - `0 <= x < capacity` for every `insert`, `remove` and `contains` call - At most `10^5` total method calls - `iterate` returns the dense sequence order, not sorted order. ### Clarifying Questions to Ask - Is `x` always guaranteed to be in range, or should out-of-range values be rejected? - Should `iterate` return a copy, or may it return a view that later calls can change? - Is the capacity fixed for the lifetime of the set? - Can the set be modified while a caller is still reading the result of `iterate`? ### What a Strong Answer Covers - The arrays the set keeps and the invariant that links them. - A membership test that stays correct with stale data in the arrays, including right after `clear`. - The removal step that keeps members packed, including removal of the value in the last slot. - Time and space cost of every method, and where the one-time allocation cost goes. - Return values and unchanged state when an insert or remove does not change membership. ### Follow-up Questions - How would you change the design if values could exceed the capacity, or the capacity had to grow? - When would a bitset be a better choice than this structure? - How would you compute the intersection of two sparse sets, and what would it cost? - How would you keep insertion order after removals while keeping every operation's time bound?

Overview: Design a sparse set over integers below a fixed capacity that supports insert, remove, membership and clear in constant time and lists its members in a required dense order. It tests array-based data structure design, invariants that hold up against stale memory, and careful complexity reasoning.

Nov 25, 2026
mediumSoftware EngineerOtherSoftware Engineering Fundamentals
0
0

Design a sparse set that stores integers x with 0 <= x < capacity. It must use O(capacity) space. Insertion, removal, membership tests and clear each run in O(1) time, and emitting the k current values takes O(k) time.

insert and remove return true only when membership changed. Inserting a value that is already present, or removing one that is missing, returns false and changes nothing. clear empties the set and returns null.

iterate returns the current dense sequence. A new value is appended to the end. Removing a present value moves the last current value into the removed value's slot. This exercise requires the stated dense sequence order. Results must not be sorted.

Implement a class SparseSet(capacity) with the following methods:

  • insert(x: int) -> bool : Add x to the end of the dense sequence if it is not already present. Return true if it was added, false if it was already present.
  • remove(x: int) -> bool : If x is present, move the last value in the dense sequence into x 's slot and shrink the sequence by one, then return true . Return false if x is not present.
  • contains(x: int) -> bool : Return true if x is currently in the set.
  • clear() : Remove all values in O(1) time.
  • iterate() -> List[int] : Return the current values in dense-sequence order, in O(k) time for k values.

Example 1:

Input:
["SparseSet", "insert", "insert", "insert", "insert", "contains", "remove", "iterate", "remove", "contains"]
[[10], [3], [5], [7], [5], [5], [3], [], [3], [3]]

Output:
[null, true, true, true, false, true, true, [7, 5], false, false]

ss = SparseSet(10)    # capacity = 10, dense: []
ss.insert(3)          # true     dense: [3]
ss.insert(5)          # true     dense: [3, 5]
ss.insert(7)          # true     dense: [3, 5, 7]
ss.insert(5)          # false    duplicate, unchanged
ss.contains(5)        # true
ss.remove(3)          # true     last value 7 fills slot 0, dense: [7, 5]
ss.iterate()          # [7, 5]
ss.remove(3)          # false    3 is missing, unchanged
ss.contains(3)        # false

Example 2:

Input:
["SparseSet", "iterate", "insert", "insert", "insert", "remove", "iterate", "clear", "iterate", "contains", "insert", "iterate", "contains"]
[[5], [], [4], [0], [2], [2], [], [], [], [4], [0], [], [4]]

Output:
[null, [], true, true, true, true, [4, 0], null, [], false, true, [0], false]

ss = SparseSet(5)     # capacity = 5, dense: []
ss.iterate()          # []       empty set
ss.insert(4)          # true     dense: [4]
ss.insert(0)          # true     dense: [4, 0]
ss.insert(2)          # true     dense: [4, 0, 2]
ss.remove(2)          # true     2 was the last element, dense: [4, 0]
ss.iterate()          # [4, 0]
ss.clear()            # null     dense: []
ss.iterate()          # []
ss.contains(4)        # false    stale data is ignored
ss.insert(0)          # true     dense: [0]
ss.iterate()          # [0]
ss.contains(4)        # false

Constraints & Assumptions

  • 1 <= capacity <= 10^5
  • 0 <= x < capacity for every insert , remove and contains call
  • At most 10^5 total method calls
  • iterate returns the dense sequence order, not sorted order.

Clarifying Questions to Ask Guidance

  • Is x always guaranteed to be in range, or should out-of-range values be rejected?
  • Should iterate return a copy, or may it return a view that later calls can change?
  • Is the capacity fixed for the lifetime of the set?
  • Can the set be modified while a caller is still reading the result of iterate ?

What a Strong Answer Covers Guidance

  • The arrays the set keeps and the invariant that links them.
  • A membership test that stays correct with stale data in the arrays, including right after clear .
  • The removal step that keeps members packed, including removal of the value in the last slot.
  • Time and space cost of every method, and where the one-time allocation cost goes.
  • Return values and unchanged state when an insert or remove does not change membership.

Follow-up Questions Guidance

  • How would you change the design if values could exceed the capacity, or the capacity had to grow?
  • When would a bitset be a better choice than this structure?
  • How would you compute the intersection of two sparse sets, and what would it cost?
  • How would you keep insertion order after removals while keeping every operation's time bound?