Data Structures & AlgorithmsVery hardTCS NQT Digital · Infosys
Problem: Consider the list a = [2, 4, 6, 8]. The loop 'for x in a: if x % 2 == 0: a.remove(x)' is run. What is the list a afterwards?
- A. [4, 6, 8]
- B. [2, 4, 6, 8]
- C. []
- D. [4, 8]
Correct answer: D. [4, 8]
Removing items while iterating shifts the remaining elements, so the iterator skips the element after each removal. Only every other element is removed, which leaves the list non-empty.
More Coding Interview Problems questions
- Problem: How many recursive calls are made in total when naive Fibonacci fib(n) = fib(n−1) + fib(n−2) with fib(0) = fib(1) = base cases…
- Problem: What is printed? print(f"{3.14159:.2f}", f"{42:05d}", f"{0.5:.0%}")
- Problem: In Python, what is the value of (−5 >> 1) + 10, where >> is the arithmetic right shift applied to the negative integer −5?
- Problem: Records (name, score) are sorted by score descending with Python's stable sort…
- Problem: A deque holds 1, 2, 3, 4, 5 from left to right. The operation rotate(2) moves the last 2 elements to the front (rotate right by…
- Problem: A 2D prefix-sum table is built for the grid [[1,2,3],[4,5,6],[7,8,9]]. What is the sum of the submatrix with rows 1–2 and columns…