Quick Overview

Count monotone grid paths that use exact numbers of right and up moves without ever taking three identical moves in a row. Address empty and one-direction paths, return an exact count, and explain how the limits affect time and space costs.

Count Grid Paths Without Three Consecutive Moves

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

## Count Paths Without Three Consecutive Moves A frog starts at `(0, 0)` and must reach `(right_steps, up_steps)`. Every move is exactly one unit right (`R`) or one unit up (`U`). A valid path may never contain `RRR` or `UUU` as a contiguous sequence. Implement: ```text count_paths(right_steps, up_steps) -> integer ``` Return the number of distinct valid move sequences that use exactly `right_steps` right moves and `up_steps` up moves. ### Constraints - `0 <= right_steps <= 20` - `0 <= up_steps <= 20` - The empty path for `(0, 0)` counts as one valid path. - Return the exact count; no modulus is used. - Paths that contain three identical consecutive moves are invalid even if the run occurs at the beginning or end. ### Examples ```text count_paths(1, 1) -> 2 ``` The valid paths are `RU` and `UR`. ```text count_paths(3, 0) -> 0 ``` The only possible sequence is `RRR`, which violates the rule. ```text count_paths(7, 6) -> 285 ``` ### Clarifications - Two paths are different when their move sequences differ. - The frog cannot move left, down, or outside the rectangle determined by the start and destination. - A run of exactly two equal moves is allowed. ### Hints - Position alone does not capture enough information to decide whether the next move is legal. - Consider tracking the previous direction and the length of its current run. - A memoized search or a bottom-up dynamic program avoids enumerating all raw paths. ### Discussion Extensions - How would the state change if at most `k` consecutive moves in one direction were allowed? - What are the time and space complexities in terms of the two step counts? - How could memory be reduced in a bottom-up implementation?

Quick Answer: Count monotone grid paths that use exact numbers of right and up moves without ever taking three identical moves in a row. Address empty and one-direction paths, return an exact count, and explain how the limits affect time and space costs.

A frog starts at (0, 0) and must reach (right_steps, up_steps). Every move is exactly one unit right (R) or one unit up (U), and the path must stay inside the rectangle from the start to the destination. Count the distinct move sequences that use exactly right_steps R moves and up_steps U moves without containing RRR or UUU as a contiguous sequence. A run of exactly two equal moves is allowed. The empty path for (0, 0) counts as one. Return the exact count with no modulus. For example, count_paths(1, 1) returns 2 for RU and UR; count_paths(3, 0) returns 0 because RRR is forbidden; and count_paths(7, 6) returns 285.

Constraints

  • right_steps and up_steps are integers.
  • 0 <= right_steps <= 20.
  • 0 <= up_steps <= 20.
  • The empty path for (0, 0) counts as one valid path.
  • Return the exact count; no modulus is used.
  • A run of exactly two equal moves is allowed, but any RRR or UUU run is invalid even at the beginning or end.

Examples

Input: (1, 1)

Expected Output: 2

Explanation: The two one-step paths are RU and UR, matching the first source example.

Input: (3, 0)

Expected Output: 0

Explanation: The only path is RRR, so the source's one-axis triple example is invalid.

Hints

  1. The current coordinates alone do not determine whether another move in the same direction is legal.
  2. Changing direction resets the consecutive-move run, while a run of two blocks only a third move in that same direction.

Loading coding console...