Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed?
Rotate a linked list to the RIGHT by k places (k may exceed the length). What is printed?
def rotate(h, k):
n, t = 1, h
while t.nx: t = t.nx; n += 1
k %= n
if k == 0: return h
t.nx = h
for _ in range(n - k): t = t.nx
h, t.nx = t.nx, None
return h
print(vals(rotate(build([1, 2, 3, 4, 5, 6]), 14)))- A. [5, 6, 1, 3, 2, 4]
- B. [5, 6, 1, 2, 3, 4]
- C. [1, 2, 3, 4, 5, 6]
- D. [3, 4, 5, 6, 1, 2]
Correct answer: B. [5, 6, 1, 2, 3, 4]
Only k mod n rotations matter. The list is made circular and cut after n − (k mod n) steps from the tail. Output: [5, 6, 1, 2, 3, 4].
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
- Over all left rotations of [7, 7, 1, 1, 3, 6], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…
- Longest substring that can be turned into one repeated letter with at most k replacements (sliding window, window − maxfreq ≤ k). What is…
- A min-stack keeps a second stack of running minimums. What is printed? python st, mn, out = [], [], [] for op in [7, 7, "pop", 3, "pop"…
- An array of 8 zeros (indices 0–7) receives range updates with a difference array (diff[l] += v, diff[r + 1] −= v, then prefix-sum): add 5…
- Count pairs with a given difference k (k > 0) among DISTINCT values using a hash set. What is printed? python a, k = [2, 11, 6, 2, 5, 9…
- LSD radix sort (base 10, stable sort on each digit, units digit first) is applied to [134, 461, 324, 794, 742, 141, 668]. What is the list…