Data Structures & AlgorithmsModerateTCS NQT Digital · Python basics
Problem: What does the following print?
Problem: What does the following print?
a = list(range(10)) print(a[::-3], a[2:8:4])
- A. [9, 6, 3] [2, 6]
- B. [0, 3, 6, 9] [2, 6]
- C. [9, 6, 3, 0] [2, 6, 8]
- D. [9, 6, 3, 0] [2, 6]
Correct answer: D. [9, 6, 3, 0] [2, 6]
a[::-3] starts at index 9 and steps back by 3 down to 0, while a[2:8:4] picks indices 2 and 6. Forgetting the endpoint 0 is a trap.
More Coding Interview Problems questions
- Problem: Count subarrays whose sum is divisible by k=5 for a = [4, 5, 0, -2, -3, 1]. python from collections import Counter def f(a, k): c…
- Problem: Kadane's algorithm is started with best = 0 and a running sum initialised to 0, then applied to an array where every number is…
- Problem: You binary search the answer x for 'the minimum possible largest sum when an array is split into k contiguous parts'. Which…
- Problem: What is the time complexity of this loop in terms of n? python for i in range(1, n + 1): for j in range(i, n + 1, i): work()
- Problem: Count right/down paths from the top-left to the bottom-right of the 3x3 grid below, where '#' is blocked. The code returns the…
- Problem: What does the following print? python def add(x, bucket=[]): bucket.append(x) return bucket add(1) add(2) print(add(3))