Linked List Random Node - Problem
Linked List Random Node is a fascinating problem that combines probability theory with data structures. You're given a singly linked list and need to implement a class that can return a random node's value from the list with equal probability for each node.

The challenge is that linked lists don't provide random access like arrays do - you can't just jump to index 3 or 7. You need to traverse from the head each time. Your task is to implement:

Solution(ListNode head) - Initialize with the linked list head
int getRandom() - Return a random node's value where each node has equal probability of being chosen

This problem tests your understanding of reservoir sampling, a clever technique for selecting random samples from a stream of unknown length. It's particularly useful when you can't store all elements in memory or when the size is unknown.

Input & Output

example_1.py โ€” Basic Usage
$ Input: list = [1, 2, 3] solution = Solution(list) solution.getRandom() # calls
โ€บ Output: Possible outputs: 1, 2, or 3 (each with equal probability)
๐Ÿ’ก Note: With 3 nodes in the list, each node should be selected approximately 1/3 of the time over many calls. The exact return value is random but the distribution should be uniform.
example_2.py โ€” Single Node
$ Input: list = [5] solution = Solution(list) solution.getRandom()
โ€บ Output: 5
๐Ÿ’ก Note: When there's only one node, that node must always be selected, so the result is deterministic.
example_3.py โ€” Multiple Calls
$ Input: list = [1, 2] solution = Solution(list) [solution.getRandom() for _ in range(6)]
โ€บ Output: Possible: [1, 2, 1, 1, 2, 2] or any other combination
๐Ÿ’ก Note: Each call is independent. With 2 nodes, over many calls we expect roughly 50% to return 1 and 50% to return 2, but individual sequences can vary.

Constraints

  • The number of nodes in the linked list will be in the range [1, 104]
  • -104 <= Node.val <= 104
  • At most 104 calls will be made to getRandom
  • Follow up: What if the linked list is extremely large and its length is unknown to you?

Visualization

Tap to expand
Reservoir Sampling: The Probability Dance1372Step 1Choose: 1Prob: 100%Step 2Replace? 1/2Each: 50%Step 3Replace? 1/3Each: 33.3%Step 4Replace? 1/4Each: 25%The Beautiful MathematicsNode 1 probability: 1 ร— (1-1/2) ร— (1-1/3) ร— (1-1/4) = 1 ร— 1/2 ร— 2/3 ร— 3/4 = 1/4 โœ“Node 2 probability: 1/2 ร— (1-1/3) ร— (1-1/4) = 1/2 ร— 2/3 ร— 3/4 = 1/4 โœ“Node 3 probability: 1/3 ร— (1-1/4) = 1/3 ร— 3/4 = 1/4 โœ“Node 4 probability: 1/4 = 1/4 โœ“Perfect uniform distribution achieved! ๐ŸŽฏ
Understanding the Visualization
1
Start the Journey
Begin with first node as your choice - it's 100% likely to be selected so far
2
Second Node Decision
Flip a fair coin (50% chance) - replace or keep? Now both nodes have equal 50% probability
3
Third Node Magic
Roll a 3-sided die - replace with 1/3 probability. All three nodes now have exactly 1/3 probability!
4
Pattern Continues
For nth node, replace with 1/n probability. Mathematics ensures perfect fairness!
Key Takeaway
๐ŸŽฏ Key Insight: Reservoir sampling proves that you don't need to know the total size to achieve perfect randomness. By using decreasing replacement probability (1/n for the nth element), each element maintains exactly 1/n final probability of selection!
Asked in
Google 42 Amazon 35 Meta 28 Microsoft 24 Apple 18
68.4K Views
Medium Frequency
~15 min Avg. Time
1.8K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen