Data Structures & AlgorithmsHardProduct-company MCQ pattern
A linked list has 3 nodes before the loop starts and a loop of 5 nodes. Floyd's slow pointer moves 1 step and fast pointer 2 steps per move, both starting at the head. After how many moves do they first meet?
- A. 5
- B. 8
- C. 15
- D. 3
Correct answer: A. 5
After k moves slow is k steps in and fast 2k; they meet when k ≥ 3 (slow is in the loop) and 2k − k = k is a multiple of 5, so k = 5. 8 = 3 + 5 is the total number of nodes, not the meeting time.
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
- ^ is exponentiation (highest precedence, right-associative). What is the postfix form of A + B C ^ D ^ E − F?
- What is the value of the postfix expression 2 3 2 ^ ^ (^ means power)?
- What is the minimum stack size needed to evaluate the postfix expression a b c d + e f − / + ?
- A queue is built from two stacks: enqueue pushes onto stack IN; dequeue pops from stack OUT, first moving everything from IN to OUT only…
- An array has only 0s, 1s and 2s. The Dutch National Flag algorithm sorts it in one pass. What are its time and extra space?
- n integers, each in the range 0 to n² − 1, must be sorted. Which approach sorts them in O(n) time?