Data Structures & AlgorithmsHardAccenture · Infosys OA

Problem: In the N-Queens backtracking search, what pruning technique keeps the search from degenerating into a full O(N^N) brute force?

  1. A. Only trying the first and last column at each row
  2. B. Sorting rows by the number of valid columns
  3. C. Tracking occupied columns and the two diagonal directions, so a queen is never placed where it would immediately conflict with a previously placed one
  4. D. Randomly shuffling the order of columns tried

Correct answer: C. Tracking occupied columns and the two diagonal directions, so a queen is never placed where it would immediately conflict with a previously placed one

By maintaining sets of used columns and both diagonals (row-col and row+col constants), the search skips entire invalid branches immediately instead of placing a queen and checking afterward, which is the core backtracking pruning.

More Coding Interview Problems questions

Exams that ask this topic