KatInterview

Search and Shortest-Path Algorithms

Trees & GraphsModel answer
MediumModel answerTrees & Graphsbinary-searchbfsshortest-path

Last updated

Implement binary search on a monotone predicate, BFS for state-space and grid shortest paths, and random board generation with adjacency counting.

You will solve four independent algorithmic problems that test search and traversal techniques:

Problem 1: Find Maximum Valid Floor

You have access to a function canOperate(floor) that returns true if floor number floor is valid for operation, and false otherwise. Floors are numbered 1 through n. There exists a threshold floor T such that all floors numbered <= T return true and all floors > T return false. Find T using at most O(log n) calls to canOperate.

Input: An integer n (the total number of floors) and access to the canOperate function.
Output: The highest floor number that returns true, or 0 if no floor is valid.


Problem 2: Minimum Moves for Combination Lock

A lock consists of four wheels, each displaying a digit from 0 to 9. On each move, you can rotate one wheel up or down by exactly one digit, with wraparound (9 + 1 = 0, 0 - 1 = 9). Some combinations are forbidden and cannot be visited during the unlocking process.

Given a starting combination, a target combination, and a set of forbidden combinations, determine the minimum number of moves required to reach the target from the start without passing through any forbidden state. If it is impossible, return -1.

Input: Strings start and target (each four digits), and a set forbidden of four-digit strings.
Output: Minimum number of moves, or -1 if unreachable.


Problem 3: Shortest Distance to Grid Boundary

You are given a rectangular grid where each cell is either open (.) or a wall (+). You start at a given open cell inside the grid. In each step, you may move to an adjacent open cell (up, down, left, or right). Find the minimum number of steps to reach any open cell on the boundary of the grid, excluding the starting cell itself. If no such exit exists, return -1.

Input: A 2D grid of characters and a starting position (row, col).
Output: Minimum steps to an open boundary cell, or -1 if impossible.


Problem 4: Random Mine Placement with Counts

Generate a minesweeper board of size rows × cols with exactly N mines placed uniformly at random in distinct cells. Each non-mine cell should contain the count of mines in its eight neighboring cells (or fewer for edge and corner cells). Use -1 to represent a mine.

Input: Integers rows, cols, and N (number of mines).
Output: A 2D list of integers representing the board.
Constraints: 0 <= N <= rows * cols

Hints

Hint 1

For the floor problem, maintain an invariant where one boundary is always valid and the other always invalid.

Hint 2

BFS explores nodes in order of increasing distance; use it when all edges have equal weight.

Hint 3

Mark cells or states as visited when you enqueue them, not when you dequeue, to avoid duplicate work.

Hint 4

Use random.sample to pick N distinct cells uniformly without replacement; avoid retry loops that degrade as density increases.