Databricks questions

Snapshot Set

DatabricksPhone screenMedium

SnapshotSet is a set of integers that remembers the order in which values were added. Removing a value and adding it again counts as a new addition, so the value moves to the end of that order.

Part 1: Ordered set

Implement the four basic methods of SnapshotSet.

  • add(value) inserts value and returns true. If value is already in the set, it returns false and changes nothing.
  • remove(value) deletes value and returns true, or returns false if value is not in the set.
  • contains(value) returns whether value is in the set.
  • values() returns a new list of the current values, oldest addition first.
  1. Example 1
    init
    []
    operations
    [["add",[3]],["add",[1]],["add",[3]],["add",[2]],["remove",[1]],["contains",[1]],["values",[]],["add",[1]],["values",[]]]
    Output
    [true,true,false,true,true,false,[3,2],true,[3,2,1]]

    Why: Adding 3 twice inserts it once, so the second add returns false. Removing 1 leaves [3, 2]; adding 1 again treats it as newly added, so it lands at the end.

Constraints

  • 0 <= value <= 10^9
  • 1 <= len(operations) <= 10^4
Snapshot Set: Databricks interview question | LoopCoach