Round 1
Questions:
You are given an array of size N
to implement a set data structure. The set should support the following operations:
- Insert(x): Insert an element
x
into the set, where0 ≤ x < N
. - Remove(x): Remove an element
x
from the set. - Lookup(x): Check if an element
x
is present in the set. - Clear(): Clear all elements from the set.
- Iterate(): Iterate over all elements currently in the set.
All operations should have a time complexity of O(1).
Constraints:
- Elements to be stored are integers in the range
[0, N-1]
. - The set should be implemented using arrays of size
N
. - The operations must be efficient, with all of them running in constant time.
Candidate's Approach
To achieve all operations in O(1) time, a data structure known as a sparse set is implemented. This structure leverages arrays and direct indexing to provide constant-time insertions, deletions, lookups, and clear operations. The implementation includes arrays for values, indices, timestamps, and counters to track the size and capacity of the set.
Interviewer's Feedback
No feedback provided.