Data Structures & AlgorithmsVery hardGATE CS · DSA
An array of 8 integers holds values 1..7 with exactly one value appearing twice: [6, 5, 7, 4, 3, 2, 1, 1]. 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 1, first meeting at index 1
- B. duplicate 1, first meeting at index 7
- C. duplicate 7, first meeting at index 1
- D. duplicate 6, first meeting at index 7
Correct answer: B. duplicate 1, first meeting at index 7
Two indices point to the duplicate value, so the functional graph has a cycle whose entry is the duplicate. Phase 1 meets at index 7; restarting one pointer from a[0] and moving both one step meets at the entry, 1.
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
- Overlapping versus non-overlapping substring counts. What is printed? python s, p = "aabaabbbabab", "bab" over = sum(1 for i in…
- Rabin–Karp with base 10 and a deliberately small modulus counts true matches and spurious hits (hash equal, text different). What is…
- A fixed window of length 3 slides over s = "baacbbbcbcabab" with a character-count array. At how many starting positions is the window an…
- Keys are inserted into a hash table of size 10 with h(k) = k mod 10 and quadratic probing (h(k) + i²) mod 10. Which statement is correct?
- What does this loop-counting code print? (Python) python n, c = 64, 0 i = n while i > 0: for j in range(i): c += 1 i //= 2 print(c)
- A 2-D prefix-sum table answers rectangle queries in O(1). For the 4 × 4 matrix (rows separated by ';', 0-based) 4 8 2 1; 4 3 1 4; 7 6 2 4…