Data Structures & AlgorithmsModerateTCS NQT · basics
Problem: From the left, how many buildings of heights [3, 1, 4, 2, 5] are visible (a building is visible if it is strictly taller than all buildings before it)? Run the code.
Problem: From the left, how many buildings of heights [3, 1, 4, 2, 5] are visible (a building is visible if it is strictly taller than all buildings before it)? Run the code.
h = [3, 1, 4, 2, 5]
mx = 0
cnt = 0
for x in h:
if x > mx:
cnt += 1
mx = x
print(cnt)What is printed?- A. 3
- B. 2
- C. 4
- D. 5
Correct answer: A. 3
The visible buildings are 3, 4 and 5. The height 1 and 2 are hidden behind taller ones.
More Coding Interview Problems questions
- Problem: For the sorted array [1, 2, 2, 2, 5, 7], how many values are at most 2? Run the code. python import bisect a = [1, 2, 2, 2, 5, 7]…
- Problem: How many strings of length 3 over the alphabet {a, b, c} have no two equal adjacent characters? Run the code. python from…
- Problem: How many divisors does 720 have? Run the code. python print(sum(1 for d in range(1, 721) if 720 % d == 0)) What is printed?
- Problem: How many ways are there to decode the digit string '111' (1 maps to A, ..., 26 maps to Z)? Run the DP. python s = '111' dp = [1]…
- Problem: What is the length of the longest substring without repeating characters in 'pwwkew'? Run the sliding-window code. python s =…
- Problem: How many distinct permutations (rearrangements as strings) of 'aabc' exist? Run the code. python from itertools import…