Python 3.11+
The examples use standard Python containers and syntax available in the supported browser runtime; Hash Set itself is taught as an interview abstraction.
Verify in Python docs(opens in a new tab)Unique-key structure for average constant-time membership and duplicate detection. This guide turns that contract into deliberate Python operations, interview invariants, and a runnable practice loop.
Recognize it when
Consider Hash Set when the prompt's constraints and required operations match this shape: Unique-key structure for average constant-time membership and duplicate detection.

Sign in to save your code and progress across devices. The lesson and problem statement remain public.
A set answers whether a distinct hashable value is present. It deliberately stores no attached count, index, or payload. This narrower contract makes intent clearer than a dictionary whose values are meaningless placeholders.
Python set supports expected O(1) membership, insertion, and removal under ordinary hashing. Keys
must be hashable and keep stable equality/hash behavior while stored. Lists and sets are mutable and
cannot be members; use a tuple or frozenset only when that immutable representation matches the
problem’s equality rule.
The set documentation(opens in a new tab)
defines add, discard, remove, and algebra operations. discard tolerates absence; remove
raises KeyError when absence violates the contract.
Test membership before insertion when the output asks whether a value appeared earlier. The
invariant is that seen contains exactly the distinct values from the processed prefix. Inserting
first would make every current value appear duplicated by itself.
def track_seen_values(values):
seen = set()
result = []
for value in values:
result.append(value in seen)
seen.add(value)
return resultImplement track_seen_values(values). Return true exactly when the current value appeared at an earlier index, before adding the current occurrence.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Intersection expresses “in both,” difference expresses “only on this side,” and union expresses “in either.” These operations return sets and therefore discard duplicates and sequence order. Convert to a sorted list only when the output contract needs deterministic ordering.
def set_relationships(left, right):
left_set = set(left)
right_set = set(right)
return (
sorted(left_set & right_set),
sorted(left_set - right_set),
sorted(left_set | right_set),
)Implement set_relationships(left, right). Return sorted shared values, values only on the left, and the full union as a tuple of lists.
Loading interactive editor.If it does not appear, the reference and starter code remain readable.Reload editor
Use a set for presence, deduplication, visited state, and algebra. Use a dictionary when each key must retain a count, original index, predecessor, or computed value. Use a boolean list when the key domain is small and dense; it may be faster and makes the bounded domain explicit.
TypeError; choose an immutable semantic key.set(values) too early loses duplicate positions and encounter order.State exactly what membership means and why no payload is needed. Define when a value enters the set and whether input order must survive. Give expected O(1) operations and O(n) space, while noting that hashing a compound key includes the work needed to hash its contents.
Track Previously Seen Values(opens in a new tab) isolates the prefix invariant. Design HashSet(opens in a new tab) then asks you to implement collision-safe membership without relying on the built-in set.
Python 3.11+
The examples use standard Python containers and syntax available in the supported browser runtime; Hash Set itself is taught as an interview abstraction.
Verify in Python docs(opens in a new tab)Interview bounds depend on the stated representation, input model, and real Python operations.
| Operation | Average | Worst | Interview note |
|---|---|---|---|
| Core Hash Set workflow | O(n) expected | O(n) expected | Membership questions about everything processed so far usually call for a hash set rather than repeated prefix scans. Before processing index i, seen contains exactly the distinct values from indices smaller than i. |
O(n) for the demonstrated Hash Set workflow.
These are the mistakes most likely to survive a happy-path example and fail a boundary case.
Placeholder code cannot preserve the Hash Set invariant or satisfy the public contract.
Prevent it: Implement the smallest complete transition and run the public tests before adding optimizations.
Avoid
def track_seen_values(values):
passUse instead
def track_seen_values(values):
seen = set()
result = []
for value in values:
result.append(value in seen)
seen.add(value)
return resultWhere you will hit this: Track Previously Seen Values(opens in a new tab)
Adds each value before testing membership, so even first occurrences are reported as repeats.
Prevent it: Keep this invariant visible while editing: State the precise Hash Set invariant before coding and preserve it after every update, traversal step, or recursive return.
Avoid
def track_seen_values(values):
seen = set()
result = []
for value in values:
seen.add(value)
result.append(value in seen)
return resultUse instead
def track_seen_values(values):
seen = set()
result = []
for value in values:
result.append(value in seen)
seen.add(value)
return resultWhere you will hit this: Track Previously Seen Values(opens in a new tab)
Handle empty state, duplicate values, aliasing, and serialization boundaries explicitly in tests.
Prevent it: Account for every slice, copy, sort, membership test, and container mutation before claiming O(n) expected.
Avoid
def track_seen_values(values):
seen = set()
result = []
for value in values:
seen.add(value)
result.append(value in seen)
return resultUse instead
def track_seen_values(values):
seen = set()
result = []
for value in values:
result.append(value in seen)
seen.add(value)
return resultWhere you will hit this: Design HashSet(opens in a new tab)
Official Python documentation supports language behavior. Canonical problem pages provide additional practice context.
python-docs · checked 2026-07-27