KatInterview

Three Algorithm Challenges: Enumeration, Backtracking, and Merging

Arrays & HashingModel answer
MediumModel answerArrays & Hashingcombinatoricsbacktrackingtwo-pointers

Last updated

Solve three distinct problems: compute the k-th element in a lexicographically ordered sequence, count valid paths under geometric constraints, and merge overlapping ranges.

You're given three independent algorithmic challenges to solve:

Challenge 1: Vehicle Registration Identifier

A vehicle registration follows a strict format: exactly 6 characters where positions 1-3 are uppercase English letters (A through Z) and positions 4-6 are decimal digits (0 through 9). For example: ABC123 or XYZ789.

If we list all possible registrations in lexicographic (dictionary) order, what is the k-th registration in this sequence? Assume k is 1-indexed.

Example: The first few registrations are AAA000, AAA001, AAA002, ..., AAA999, AAB000, ...

Challenge 2: Grid Pattern Combinations

Consider a 3×3 grid of points numbered like a phone keypad:

1 2 3
4 5 6
7 8 9

You must create patterns by connecting points with straight lines. Rules:

  • Each point can be used at most once per pattern
  • When connecting point A to point B, if another point C lies directly on the line segment between them, you cannot make this connection unless C was already used earlier in the pattern
  • Knight-move style connections (like 1→6) are always valid since no point lies between them

Calculate how many distinct valid patterns exist that use between 4 and 9 points (inclusive).

Challenge 3: Overlapping Time Ranges

You receive two lists of time intervals, first and second. Each interval is represented as [start, end] where both endpoints are included. Within each list:

  • Intervals are sorted by start time
  • No two intervals overlap or touch

Find all time periods where intervals from first and intervals from second overlap.

Example:

first = [[0,4], [6,10], [12,18]]
second = [[2,6], [8,14]]
Output: [[2,4], [6,6], [8,10], [12,14]]

Hints

Hint 1

Challenge 1: Think about positional number systems. Each character position has a fixed set of possible values.

Hint 2

Challenge 2: Use depth-first exploration with a lookup table for which point lies between any two given points. Exploit grid symmetry to reduce computation.

Hint 3

Challenge 3: Use two pointers advancing through both lists. The overlap of two intervals can be computed directly from their endpoints.