UBER Coding Question – Solved

9 Live
Bitwise Palindromic Paths You are given a binary matrix of size N x M where each cell contains either 0 or 1. A path from the top-left cell (0,0) to the bottom-right cell (N-1,M-1) is valid if you can only move right or down at each step. A path is called bitwise palindromic if the sequence of bits collected along the path forms a palindrome when interpreted as a string (e.g., 0110, 1, 101). Your task is to count the number of such bitwise palindromic paths. Function Description: You need to implement the function countBitwisePalindromicPaths. Parameters: . N: An integer representing the number of rows. · M: An integer representing the number of columns. · grid: A 2D list of size N x M consisting of binary values (0 or 1). Return: An integer representing the number of valid bitwise palindromic paths from (0,0) to (N-1,M-1), modulo 10⁹+7. Input Format: · The first line contains a single integer N. . The second line contains a single integer M. . The next N lines each contain M space-separated binary digits · 1 ≤ N, M ≤ 15 Sample input 2 2 1 0 0 1 Sample output 2 Explanation There are 2 paths: . Path 1: 1 → 0 → 1 forms "101", which is a palindrome . Path 2: 1 → 0 → 1 – same path, same pattern, counted again Note: Your code must be able to print the sample output from the provided sample input. However, your code is run against multiple hidden test cases. Therefore, your code must pass these hidden test cases to solve the problem statement. Limits Time Limit: 5.0 sec(s) for each input file Memory Limit: 256 MB Source Limit: 1024 KB Scoring Score is assigned if any test case passes Allowed Languages

Asked in: UBER

Image of the Question

Question Image Question Image

All Testcases Passed ✔



Passcode Image

Solution


from functools import lru_cache
def countBitwisePalindromicPaths (N, M, grid):
    mod = 10**9+7
    @lru_cache(None)
// ... rest of solution available after purchase

🔒 Please login to view the solution

Explanation


```
To solve the problem of counting the number of bitwise palindromic paths from the top-left corner to the bottom-right corner of a binary grid, where movement is restricted to right or down steps, you need to carefully consider both the nature of palindromes and the constraints of the grid traversal.

---

Step 1: Understand the Path and Palindrome Structure

- A path is defined by a sequence of moves starting at (0,0) and ending at (N-1, M-1).
- You can move only right or down, so every path corresponds to a unique sequence of bits from cells visited.
- The total length of any path is fixed: it will always have exactly (N + M - 1) bits because you start at (0,0) and move (N-1) steps down and (M-1) steps right in some order.
- The palindrome condition means this sequence of bits must read the same forwards and backwards.

---

Step 2: Observing the Palindromic Path Property

- Since the path length is fixed, the palindrome condition requires symmetry in the bits from the start and end positions.
- Imagine the path sequence indexed from 0 to L-1 (where L = N + M - 1).
- For the path to be palindromic, the bit at index i must be equal to the bit at index L - 1 - i.
- This symmetry means the bit collected at the ith step from the start corresponds to the bit collected at the ith step from the end.
- This crucial insight allows us to think of the problem in a bidirectional manner.

---

Step 3: Two-Pointer / Two-Ends Approach

- Instead of enumerating all paths from start to end (which can be exponential), think about simultaneously traversing from the start cell and from the end cell moving towards the middle.
- At each step, you keep track of pairs of positions:
- One position moving forward (starting from top-left)
- One position moving backward (starting from bottom-right)
- For the path to be palindromic, the bits at these two positions must be the same.
- The two positions are always chosen such that they correspond to symmetric indices in the path sequence.

---

Step 4: Dynamic Programming State Definition

- Define a DP state that records the number of ways to reach a pair of positions (r1, c1) and (r2, c2), where (r1, c1) is a cell reachable from the start, and (r2, c2) is a cell reachable from the end.
- The positions satisfy that the number of steps from start to (r1, c1) plus the number of steps from (r2, c2) to end equals L - 1.
- This means these positions are "mirrored" in the path.
- You maintain a count of how many paths can reach these two positions with bits matching, ensuring palindrome property.

---

Step 5: Transitions in the DP

- From a current pair of positions, you consider moving the start position either right or down (if possible).
- Similarly, you move the end position either left or up (if possible).
- For each possible move, if the bits at the new positions are equal, you add the count from the current state to the new state.
- This way, you propagate counts of palindromic path fragments inward from both ends.

---

Step 6: Base Cases

- The initial DP state corresponds to the pair of positions both at the start and end cells: (0,0) and (N-1,M-1).
- If the bits at these cells are equal, you initialize that DP state with count 1.
- If they are not equal, the answer is immediately zero.

---

Step 7: Ending Condition and Result

- The process continues until the two pointers meet or cross.
- When the pointers meet (they may be at the same cell or adjacent cells, depending on odd or even path length), the DP states represent fully palindromic paths.
- Sum all DP counts for valid meeting positions to get the total number of palindromic paths.
- Return the count modulo 10^9+7 as per the problem constraints.

---

Step 8: Complexity and Feasibility

- Since N and M are up to 15, the total grid size is small enough to allow a DP over pairs of positions.
- The number of states is roughly O(N^2 * M^2), which is manageable.
- Efficient implementation and pruning (only states where bits match are considered) helps performance.

---

Step 9: Summary of the Approach

- Think about the palindrome property as symmetry between start and end positions.
- Use two pointers: one advancing from the top-left, one retreating from the bottom-right.
- Use dynamic programming to count the number of ways to reach each pair of positions with matching bits.
- Accumulate counts until pointers meet.
- Return the total count modulo the large prime.

By approaching the problem as a symmetric traversal with two pointers and applying DP over pairs of positions, you transform an exponential path counting problem into a tractable and elegant dynamic programming solution that leverages the palindrome constraints naturally.
```


Related Questions