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]), 9)))- A. [4, 5, 6, 1, 3, 2]
- B. [5, 4, 6, 1, 2, 3]
- C. [5, 6, 1, 2, 3, 4]
- D. [4, 5, 6, 1, 2, 3]
Correct answer: D. [4, 5, 6, 1, 2, 3]
Only k mod n rotations matter. The list is made circular and cut after n − (k mod n) steps from the tail. Output: [4, 5, 6, 1, 2, 3].
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
- Remove the n-th node from the END of a linked list in one pass, using a dummy node and a gap of n between two pointers. What is printed?…
- Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than…
- Evaluate the postfix expression 7 4 − 2 + 6 − 8 × 7 1 + + (all divisions are exact). What is its value, and what is the maximum number of…
- KMP failure function (lps[i] = length of the longest proper prefix of p[0..i] that is also a suffix). What is printed? python def lps(p)…
- A sorted array was rotated to give [58, 67, 70, 71, 77, 3, 12, 14, 55]. Binary search finds the index of the minimum: while lo < hi, mid =…
- A string s has length n. What is the time complexity of building s by repeated concatenation s = s + c in a language with immutable…