Data Structures & AlgorithmsHardProduct-company MCQ pattern
What does this function print for the list 1 → 2 → 3 → 4 → 5 → 6?
What does this function print for the list 1 → 2 → 3 → 4 → 5 → 6?
def f(node):
if node is None:
return
if node.next:
f(node.next.next)
print(node.val, end=" ")
f(head)- A. 5 3 1
- B. 2 4 6
- C. 6 4 2
- D. 1 3 5
Correct answer: A. 5 3 1
f(1) calls f(3), which calls f(5); 5.next is 6 and 6.next is None, so f(None) returns. Printing happens after the call, as the recursion unwinds: 5, then 3, then 1. Printing before the call would give 1 3 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
- slow and fast both start at the head of 1 → 2 → 3 → 4 → 5 → 6. Loop A: while (fast && fast->next) { slow = slow->next; fast =…
- What is the maximum number of comparisons top-down merge sort can make on 8 elements (any input order)?
- What is the minimum number of comparisons needed, in the worst case, to find BOTH the maximum and the minimum of 100 distinct numbers?
- One Lomuto partition (pivot = last element, i marks the end of the 'smaller' part, swap when a[j] < pivot) is applied to [7, 2, 9, 4, 3…
- Insertion sort (ascending) is applied to [5, 2, 4, 6, 1, 3]. What does the array look like after the first 3 passes (i.e. after inserting…
- Among insertion sort, merge sort, heap sort, quick sort, bubble sort and selection sort (standard array versions), how many are BOTH…