Data Structures & AlgorithmsVery hardTCS NQT Digital · Product Company SDE

Problem: In a spiral traversal of a 5×5 matrix filled row-wise with 1 to 25, the traversal starts at the top-left and goes right, then down, then left, then up, shrinking the boundary each time. What is the sum of the first 13 values visited?

  1. A. 175
  2. B. 191
  3. C. 199
  4. D. 91

Correct answer: A. 175

The spiral visits 1-5, then 10, 15, 20, 25, then 24, 23, 22, 21: these 13 values sum to 175. Taking 14 values adds 16 (191), going down first (counter-clockwise) gives 199, and reading the first 13 values row-wise gives 91.

More Coding Interview Problems questions

Exams that ask this topic