Data Structures & AlgorithmsModerateTCS NQT · array basics
Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the code.
Problem: How many peaks (elements strictly greater than both neighbours, excluding the two ends) does [1, 3, 2, 4, 1, 5] have? Run the code.
a = [1, 3, 2, 4, 1, 5] print(sum(1 for i in range(1, len(a) - 1) if a[i] > a[i - 1] and a[i] > a[i + 1]))What is printed?
- A. 1
- B. 2
- C. 3
- D. 0
Correct answer: B. 2
The peaks are 3 at index 1 and 4 at index 3. The value 5 is at an end and cannot be a peak under this definition.
More Coding Interview Problems questions
- Problem: What does this run-length encoder print for 'aaabccdd'? python s = 'aaabccdd' out = [] i = 0 while i < len(s): j = i while j <…
- Problem: Check bracket balance for '([]{})' and '([)]' with a stack. What is printed? python def ok(s): pairs = {')': '(', ']': '[', '}'…
- Problem: Sum the values of the next greater element of each position in [2, 1, 4, 3], where a position with no greater element contributes…
- Problem: Using at most one flip of a 0 to 1, what is the longest run of consecutive 1s in [1, 0, 1, 1, 0, 1]? Run the sliding-window code…
- Problem: How many unordered pairs of words in ['abc', 'bca', 'xyz', 'cab'] are anagrams of each other? Run the code. python from itertools…
- Problem: How many subsets of [1, 2, 3, 4, 5] have a sum of exactly 7? Run the code. python from itertools import combinations a = [1, 2…