KatInterview

Three-Problem Algorithmic Challenge Set

Arrays & HashingModel answer
MediumModel answerArrays & Hashingsimulationgraph-traversalbit-manipulation

Last updated

Solve three independent problems: tournament match counting, grid traversal with movement commands, and finding maximum sum of digit-disjoint pairs.

You have 150 minutes to solve three independent coding problems. Each problem stands alone with its own input and output.


Problem 1: Tournament Match Participation

You have $n$ competitors arranged in sequence, numbered $1$ through $n$. Each competitor $i$ possesses a unique strength level $a[i]$, where the values form a permutation of $1$ to $n$.

The tournament operates in elimination rounds:

  1. Each round pairs adjacent survivors: position $1$ faces $2$, position $3$ faces $4$, etc.
  2. Within each pairing, the competitor with greater strength advances; the weaker one is eliminated.
  3. Winners maintain their relative ordering and form the lineup for the subsequent round.
  4. This continues until a single champion remains.

For each original competitor $i$, compute $b[i]$ = the total number of matches competitor $i$ fought before elimination (or through championship).

Input: Integer $n$ (a power of 2, $1 \le n \le 200{,}000$) and array $a[1..n]$ (a permutation of $1..n$).

Output: Array $b[1..n]$ where $b[i]$ is the match count for competitor $i$.


Problem 2: Grid Navigation Instructions

You control a robot on an $R \times C$ grid ($3 \le R, C \le 50$) where:

  • # = impassable wall
  • . = traversable floor
  • * = robot's starting position (also traversable)

The perimeter is entirely walls. All floor cells (including *) form one connected region.

Generate a sequence of movement commands (^ up, v down, < left, > right) for each scenario below. Every move must keep the robot on floor cells. The sequence may be at most $100{,}000$ characters. You need not return to start.

Five Scenarios:

  1. Perimeter only: Floor cells form a solid rectangle. Start is the top-left corner. Visit every perimeter cell exactly once; never enter interior cells.

  2. Full rectangle: Floor cells form a solid rectangle. Start anywhere inside. Visit every floor cell at least once.

  3. Two-room corridor: Two separate rectangular regions connected by exactly one corridor cell. Start anywhere. Visit all floor cells.

  4. Linear path: Floor cells form a path with no branches. Start anywhere on the path. Visit every cell.

  5. General shape: Any connected floor region. Start anywhere. Visit every floor cell.

Output: For each scenario, provide one valid command string meeting the requirements, length $\le 100{,}000$.


Problem 3: Digit-Disjoint Maximum Pair Sum

Given an array of $n$ non-negative integers $a[1..n]$ ($1 \le n \le 200{,}000$, each $a[i] \le 10^9$).

Two numbers have disjoint digits if their decimal representations share no common digit. For example, $123$ and $4560$ are disjoint; $15$ and $51$ are not (both contain $1$ and $5$).

Task: Find two distinct indices $i \ne j$ where $a[i]$ and $a[j]$ have disjoint digits, maximizing $a[i] + a[j]$. Return this maximum sum, or $-1$ if no valid pair exists.

Input: Array $a[1..n]$.

Output: Maximum sum of a valid pair, or $-1$ if impossible.

Hints

Hint 1

For the tournament, think about how many rounds each competitor survives rather than simulating individual matchups.

Hint 2

Grid traversal can use depth-first search with backtracking—each edge traversed twice (forward and return) gives a tight bound.

Hint 3

Represent each number's digit set as a 10-bit mask; you only need to track the maximum value for each unique mask.