One, Two, Skip a few...
10s 256 MB
Implement SkipList, a probabilistic sorted data structure that supports fast search, insertion, and deletion by maintaining multiple layers of linked lists with express lanes.
This problem focuses on layered data structure mechanics and correct PRNG-driven level generation.
Constructor
SkipList(seed)
seedis anunsigned 32-bit integer.- Initializes an empty skip list with the given PRNG seed.
- The skip list should support up to 16 levels (levels 0 through 15).
- Level 0 is the bottom level containing all elements.
Level Generation
Each time a new value is successfully inserted (not a duplicate), generate its level as follows:
Advance the PRNG state before using it:
The multiplication and addition should be performed using unsigned 32-bit arithmetic (i.e. mod 2^32).seed = seed * 1103515245 + 12345Compute the level by counting the number of trailing 1-bits in the updated
seed, capped at 15.- Example: if
seedin binary ends in...0111, the level is3. - If
seedis0, the level is0. - If all 32 bits are
1, the level is15(capped).
- Example: if
The PRNG state is shared across all insertions and advances only on successful inserts (not on duplicates, removes, searches, or displays).
Methods
insert(value)
valueis aninttype.- If
valuealready exists in the skip list, do nothing (do not advance the PRNG). - Otherwise, generate a level using the procedure above, then insert the node into levels 0 through that level (inclusive).
- Print
null.
remove(value)
valueis aninttype.- Removes
valuefrom all levels of the skip list. - If
valuedoes not exist, do nothing. - Print
null.
search(value)
valueis aninttype.- Returns
trueifvalueexists in the skip list,falseotherwise. - The search must start from the highest occupied level and traverse forward and down — not simply scan level 0.
display()
- Prints each occupied level of the skip list on its own line, from the highest level down to level 0.
- Each line should be formatted as
Level {L}:followed by the values on that level in ascending order, space-separated. - If the skip list is empty, print nothing (an empty line).
Notes
- The PRNG is a standard linear congruential generator. Getting the unsigned 32-bit wraparound correct is critical.
- How you represent nodes and forward pointers is up to you.
- You may assume single-threaded usage.
Constraints
0 <= seed <= 2^32 - 11 <= value <= 10^5- At most
10^4total operations.
Examples
Example 1
Input:
SkipList 42
insert 3
insert 6
insert 7
insert 9
insert 12
display
search 7
search 10Output:
null
null
null
null
null
Level 3: 12
Level 2: 3 12
Level 1: 3 7 12
Level 0: 3 6 7 9 12
true
falseAccepted 1/1
Acceptance 100%
Loading editor...
Input
SkipList 42
insert 3
insert 6
insert 7
insert 9
insert 12
display
search 7
search 10Expected Output
null
null
null
null
null
Level 3: 12
Level 2: 3 12
Level 1: 3 7 12
Level 0: 3 6 7 9 12
true
false