Data Structures & AlgorithmsModerateProduct placement · Amazon/Microsoft
Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed?
Delete every alternate node (2nd, 4th, …) of a linked list, then reverse what remains. What is printed?
def del_alt(h):
p = h
while p and p.nx:
p.nx = p.nx.nx
p = p.nx
return h
def rev(h):
prev = None
while h: h.nx, prev, h = prev, h, h.nx
return prev
print(vals(rev(del_alt(build([36, 47, 37, 15, 40, 26, 24])))))- A. [40, 24, 37, 36]
- B. [24, 37, 40, 36]
- C. [24, 40, 37, 36]
- D. [36]
Correct answer: C. [24, 40, 37, 36]
After each deletion p jumps to the node that followed the deleted one, so positions 1, 3, 5, … survive; reversing them gives [24, 40, 37, 36].
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
- Asteroids move along a line: positive values move right, negative move left; on collision the smaller (by absolute value) explodes, equal…
- Lines of heights [9, 5, 3, 2, 4, 9, 1, 2, 9] stand at x = 0, 1, …, 8. Two pointers find the pair of lines that, with the x-axis, holds the…
- How many different pop sequences are possible when 6 distinct items are pushed onto a stack in a fixed order, with pops allowed at any time?
- Count zero-sum contiguous subarrays with a prefix-sum frequency map. What is printed? python from collections import defaultdict a = [1…
- Daily prices are [26, 31, 28, 30, 24, 30, 18, 37, 13]. What is the maximum profit with at most ONE buy and one later sell, and with…
- Floyd's cycle detection on a list whose last node links back into the list. What does this print (meeting value, cycle-start value, cycle…