Coding interviews
Reveal answer and reasoning
For a concurrently maintained list, the most straightforward contract is that the caller owns an immutable snapshot or holds the required structural lock during inspection. A visited set alone does not repair concurrent rewiring. The senior tradeoff is therefore not just memory versus speed, but whether a stable graph exists to inspect.
With an unchanged finite reachable structure, the algorithm uses O(n) pointer advances and O(1) auxiliary references. A visited-node set is a valid alternative with O(n) storage and often a simpler first explanation. Returning only Value loses identity information when several nodes share a value. Overriding value equality on a node type can make == or an ordinary equality-based set unsuitable for identity detection.
ReferenceEquals states the contract directly. Concurrent link mutation invalidates the usual proof, and this sample provides neither synchronization nor a snapshot of a shared list.
A rigorous answer distinguishes repeated values from repeated nodes, supplies a termination argument for both acyclic and cyclic input, and explains why resetting one cursor locates the entry. It also recognizes that a set can be an acceptable initial solution when memory is available and clarity matters. The tested fixture intentionally hides identity behind duplicate displayed values, so output inspection alone is insufficient for richer tests.
Demonstrate self-loop, head-entry and non-head-entry cases with reference assertions. These checks test the actual returned object, which is the central contract of this problem.
What a strong answer covers
- For a concurrently maintained list, the most straightforward contract is that the caller owns an immutable snapshot or holds the required structural lock during inspection.
- With an unchanged finite reachable structure, the algorithm uses O(n) pointer advances and O(1) auxiliary references.
- Overriding value equality on a node type can make == or an ordinary equality-based set unsuitable for identity detection.
Common pitfall
Overriding value equality on a node type can make == or an ordinary equality-based set unsuitable for identity detection. ReferenceEquals states the contract directly. Concurrent link mutation invalidates the usual proof, and this sample provides neither synchronization nor a snapshot of a shared list.
Follow-up questions
- Add a self-loop and a cycle whose entry is the head. Hint: test the returned object with ReferenceEquals rather than checking its Value. Keep a repeated-value acyclic fixture to catch accidental equality-based detection.
- Which assumption fails if another thread rewires Next during cycle detection?
Self-review
Choose what to revisit. These marks are your own assessment and are separate from lesson completion.