Data Structures & AlgorithmsModerateTCS NQT · Python basics
Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order.
Problem: What does the following print? It sums the entries at even positions of the array sorted in descending order.
a = [5, 1, 9, 3] print(sum(sorted(a, reverse=True)[::2]))What is printed?
- A. 14
- B. 12
- C. 10
- D. 8
Correct answer: B. 12
Sorted descending gives [9, 5, 3, 1], and the even positions hold 9 and 3, totalling 12. Summing the odd positions (5 + 1 = 6) is the trap.
More Coding Interview Problems questions
- Problem: What is the maximum sum of non-adjacent elements of [3, 2, 7, 10]? Run the DP. python a = [3, 2, 7, 10] inc = exc = 0 for x in a…
- Problem: How many ways tile a 1 x 4 strip with 1 x 1 squares and 1 x 2 dominoes? Run the code. python f = [1, 1] for in range(3)…
- Problem: How many trailing zero bits does 40 have in binary? Run the code. python print((40 & -40).bitlength() - 1) What is printed?
- Problem: How many pairs (i < j) of [2, 4, 6, 8] have gcd exactly 2? Run the code. python from itertools import combinations from math…
- Problem: Starting at (0,0) in the grid below, how many cells are reachable by 4-directional moves through '.' cells? Run the BFS. python…
- Problem: What is the minimum number of coins from {1, 5, 10, 25} summing to 63? Run the DP. python INF = 10 9 d = [0] + [INF] 63 for v in…