Count Grid Paths Without Three Consecutive Moves

Last updated: July 28, 2026

Quick Overview

Given a grid, the task is to count the number of unique paths from the top-left corner to the bottom-right corner without making three consecutive moves in the same direction.

Anonymous
Coding & Algorithms
Software Engineer
Anonymous
July 28, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Medium

2

0

30 solved


Given a grid, the task is to count the number of unique paths from the top-left corner to the bottom-right corner without making three consecutive moves in the same direction.

This question is commonly asked during online assessments to evaluate a candidate's problem-solving skills and understanding of dynamic programming. The interviewer is looking for the candidate's ability to break down the problem, implement an efficient algorithm, and handle edge cases effectively. It tests both coding proficiency and algorithmic thinking.

What the Interviewer Expects
  • Identify the constraints of the problem clearly.
  • Utilize dynamic programming to optimize the solution.
  • Provide a clear explanation of the approach taken.
  • Discuss time and space complexity of the solution.
  • Handle edge cases and provide test cases.
Key Topics to Cover
Dynamic Programming
Grid Traversal
Recursion
Path Counting
Complexity Analysis
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you modify the solution for a larger grid?
  • Can you explain how you would handle different movement constraints?
  • What would you do if the grid had obstacles?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Approach

To solve the problem of counting unique paths in a grid while avoiding three consecutive moves in the same direction, we can use dynamic programming. The idea is to maintain a 3D DP array where `dp[x]...

Algorithm
  1. Initialize a 3D DP array with dimensions (m+1) x (n+1) x 4 where m and n are the grid dimensions.
  2. Set the starting position dp[0][0][0], dp[0][0][1], dp[0][0][2], and dp[0][0][3] t...

Submit Your Answer
Markdown supported

Related Questions