Data Structures & AlgorithmsHardFlipkart/Uber OA · placement
Delete duplicates from a SORTED linked list. Version A keeps one copy of each value; version B removes every value that appears more than once. 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 once. What is printed?
def keep_one(h):
c = h
while c and c.nx:
if c.v == c.nx.v: c.nx = c.nx.nx
else: c = c.nx
return h
def drop_all(h):
d = p = N(0, h)
while p.nx:
q = p.nx
if q.nx and q.v == q.nx.v:
while q.nx and q.v == q.nx.v: q = q.nx
p.nx = q.nx
else: p = p.nx
return d.nx
print(vals(keep_one(build([1, 1, 2, 2, 4, 4, 6, 6, 6]))), vals(drop_all(build([1, 1, 2, 2, 4, 4, 6, 6, 6]))))- A. [1, 2, 4, 6, 6] []
- B. [1, 2, 4, 6] []
- C. [1, 4, 2, 6] []
- D. [2, 1, 4, 6] []
Correct answer: B. [1, 2, 4, 6] []
keep_one advances only when the next value differs, so runs of 3+ copies collapse fully; drop_all skips a whole run and links past it. Output: [1, 2, 4, 6] [].
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
- 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…
- What does this loop-counting code print? (Python) python n, c = 10, 0 for i in range(n): for j in range(i + 1, n): for k in range(j + 1…
- Over all left rotations of [5, 1, 5, 3, 9, 4], what is the maximum value of Σ i·a[i] (0-based)? (Use R(j) = R(j − 1) + sum − n·a[n − j] to…