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
July 28, 20262
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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- 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 ProblemsSample 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
- Initialize a 3D DP array with dimensions
(m+1) x (n+1) x 4wheremandnare the grid dimensions. - Set the starting position
dp[0][0][0],dp[0][0][1],dp[0][0][2], anddp[0][0][3]t...