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?
- A. Only trying the first and last column at each row
- B. Sorting rows by the number of valid columns
- 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
- 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
- Problem: Which best explains why a monotonic stack is the standard technique for the 'next greater element' problem, giving O(n) overall…
- Problem: A sliding window maximum over an array of size n with window size k is computed with a monotonic deque. What is its time…
- Problem: Two Sum on an unsorted array can be solved in O(n) using a hash map, or in O(n log n) using sort-and-two-pointers. Why might the…
- Problem: Three Sum (find all triplets summing to zero) is commonly solved by sorting the array and then applying two pointers for each…
- Problem: Which statement about Dijkstra's algorithm and negative edge weights is correct?
- Problem: Which best describes the relationship between DFS-based cycle detection in a directed graph and the 'white-gray-black' (or…