Data Structures & AlgorithmsVery hardTCS NQT · Amazon coding round
In a linked list the first 5 nodes form a tail, and the next 7 nodes form a cycle (the last node points back to the 6th node). Floyd's algorithm starts with slow and fast pointers both at the head; each iteration moves slow by one node and fast by two, and then compares them. After how many iterations are they first equal?
- A. 5
- B. 7
- C. 12
- D. 14
Correct answer: B. 7
Once both pointers are in the cycle, fast gains one node per step on slow. They meet at the first iteration t that is at least the tail length 5 and a multiple of the cycle length 7, giving t = 7 (and not tail + cycle = 12).
Practise more Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questionsArrays, Strings, Linked Lists, Stacks, Queues and Hashing: notes and key points
More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions
- For each element of the array [6, 2, 5, 4, 7, 3, 1, 8, 2] find the next greater element to its right (the value -1 if none exists). What…
- A hash table of size 11 uses h(k) = k mod 11 with linear probing. Keys 5, 16, 27 and 38 are inserted in this order, and then key 16 is…
- For the array [1, 3, -1, -3, 5, 3, 6, 7, 2, 9], consider every window of 4 consecutive elements. The sum of the maximum of each window is:
- A circular queue uses an array of size 5 with the convention that front is the index of the next element to dequeue, rear is the index of…
- The integers 1, 2, 3, 4, 5, 6 are pushed onto an initially empty stack in this order; pops may happen at any time, and the 6 popped values…
- A hash table of size 11 uses double hashing: the probe sequence for key k is (k mod 11 + i · (7 − (k mod 7))) mod 11 for i = 0, 1, 2…