Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed?
Reverse a singly linked list in groups of k (a final group shorter than k is left as it is). What is printed?
def rev_k(h, k):
cnt, p = 0, h
while p and cnt < k: p = p.nx; cnt += 1
if cnt < k: return h
prev, cur = rev_k(p, k), h
for _ in range(k):
cur.nx, prev, cur = prev, cur, cur.nx
return prev
print(vals(rev_k(build([1, 2, 3, 4, 5, 6, 7, 8]), 4)))- A. [4, 3, 2, 1]
- B. [4, 3, 2, 1, 8, 7, 6, 5]
- C. [4, 3, 2, 1, 5, 6, 7, 8]
- D. [3, 4, 2, 1, 8, 7, 6, 5]
Correct answer: B. [4, 3, 2, 1, 8, 7, 6, 5]
Each full block of k nodes is reversed and linked to the already-processed remainder; the short tail block keeps its order. Output: [4, 3, 2, 1, 8, 7, 6, 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
- Merge two sorted linked lists by relinking nodes (ties take the node from the first list). What is printed? python def merge(a, b): d = t…
- Print a matrix in spiral order (right, down, left, up, repeat). What is printed? python m = [[90, 11, 62, 33], [40, 71, 82, 13], [10, 81…
- A 2-D array A[0..11][0..29] of 4-byte elements is stored in row-major order starting at address 1000. What is the address of A[5][11]?
- For the sorted array [1, 5, 6, 7, 7, 7, 8, 9, 9, 10, 10, 11] (0-based), what are lowerbound(9) (first index with value ≥ 9) and…
- Keys 13, 14, 24, 27, 70, 76, 91, 15 are inserted in this order into an empty hash table of size 13 with h(k) = k mod 13 and linear…
- Keys 55, 63, 47, 69, 16, 97, 39, 99, 77, 43 are inserted in this order into a chained hash table with 7 buckets, h(k) = k mod 7, and each…