Detect a Linked-List Cycle
Updated
Problem statement
A linked list is encoded by nextIndex: node i points to node nextIndex[i], and -1 marks the end. Traversal starts at node 0.
Return whether the reachable chain contains a cycle. An empty array represents an empty list.
Function
hasCycle(nextIndex: int[]) → booleanExamples
Example 1
nextIndex = [-1]return = falseCase 1 exercises the documented deterministic contract.
Example 2
nextIndex = [1,2,-1]return = falseCase 2 exercises the documented deterministic contract.
Example 3
nextIndex = [1,2,0]return = trueCase 3 exercises the documented deterministic contract.
Constraints
0 <= nextIndex.length <= 200000.- Every value is
-1or a valid node index.
Source note: Author-written prompt excerpt retained for source fidelity.
More Paytm interview questions
- Find First and Last Position in Sorted ArrayONSITE INTERVIEW · Seen Sep 2026
- Minimum Coins for a TargetONSITE INTERVIEW · Seen Sep 2026
- Nth Highest SalaryPHONE SCREEN · Seen Nov 2026
- Second Highest SalaryPHONE SCREEN · Seen Nov 2026
- Jump GamePHONE SCREEN · Seen Nov 2026
- LRU Cache with Expiring EntriesPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Detect a Linked-List CycleCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Detect a Linked-List CycleMicrosoft · ONSITE INTERVIEWMicrosoft interview questions
- Detect and Remove a Linked List LoopBank of America · ONSITE INTERVIEWBank of America interview questions
- Find a Linked-List Cycle EntryExpedia · ONSITE INTERVIEWExpedia interview questions
- First Node Two Linked Lists ShareAdobe · PHONE SCREENAdobe interview questions
- Check a Linked List for a CycleZillow · PHONE SCREENZillow interview questions
- Where Two Linked Lists MeetNutanix · PHONE SCREENNutanix interview questions
- LRU CacheApple · PHONE SCREEN · ONSITE INTERVIEWApple interview questions
- LRU Cache for Query ResultsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- LRU CacheServiceNow · PHONE SCREEN · ONSITE INTERVIEWServiceNow interview questions