Data Structures & AlgorithmsVery hardGATE CS · DSA
An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 5, 1, 2, 3, 7, 4]. Treating i → a[i] as a linked list from index 0, Floyd's cycle detection returns the duplicate. Which value is it, and at which index do slow (one step) and fast (two steps) first meet (both start at index a[0])?
- A. duplicate 5, first meeting at index 7
- B. duplicate 6, first meeting at index 1
- C. duplicate 1, first meeting at index 5
- D. duplicate 5, first meeting at index 1
Correct answer: D. duplicate 5, first meeting at index 1
Two indices point to the duplicate value, so the functional graph has a cycle whose entry is the duplicate. Phase 1 meets at index 1; restarting one pointer from a[0] and moving both one step meets at the entry, 5.
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
- Two strings are isomorphic if characters can be replaced one-to-one to turn one into the other. Version A checks both directions; version…
- Trains occupy a platform from arrival to departure INCLUSIVE, so a train arriving at the same minute another departs needs a separate…
- Which statement about the KMP failure (lps) array of a pattern of length m is correct?
- Find the leaders (elements strictly greater than everything to their right) by scanning from the right. What is printed? python a = [18…
- Integer square root by binary search (largest m with m·m ≤ n). What is printed (root, iterations)? python n, lo, hi, it = 200, 0, 200, 0…
- Items E, F, G, H, I, J are pushed onto an empty stack in this order, and a pop may happen at any time. Which pop order is NOT possible?