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?
  1. A. 3
  2. B. 2
  3. C. 4
  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

Exams that ask this topic