This time on HackerRank I came across a classic set of Tiktok OA , a set of three questions covering the three main sections of DP, complete backpacking, and BFS state compression. The difficulty is not outrageous, but the requirements for code proficiency and detail control are relatively high. If you don't pay attention to these boundary conditions, it's easy to fall into the pit. Here is a review in the order of the questions.
Question 1 - Robot Paths with Landmines
Problem
A robot is moving on a grid of size n×mn \times mn×m. In one move, it moves one step right or one step down. Some cells of the grid have landmines, so the robot has Some cells of the grid have landmines, so the robot has to avoid them. 0 means landmine and 1 Find the number of ways to reach the bottom-right cell from the top-left cell, modulo 109+710^9 + 7109+7.
Ideas
Typical DP model:dp[i][j] Indicates the number of paths to the point
If the current cell is a mine, just dp[i][j]=0
Transfer formula: add up the number of paths from above and to the left
Remember to check the results. % MOD
Core code (Python)
MOD = 10**9 + 7
def findSafeWays(grid): n, m = len(grid), len(grid[0])
n, m = len(grid), len(grid[0])
if grid[0][0] == 0 or grid[n-1][m-1] == 0.
return 0
dp = [0] * m
dp[0] = 1
for j in range(1, m).
dp[j] = dp[j-1] if grid[0][j] else 0
for i in range(1, n): dp[0] = dp[j-1] if grid[0][j] else 0
dp[0] = dp[0] if grid[i][0] else 0
for j in range(1, m): if grid[i][j][0] else 0
if grid[i][j] == 0.
dp[j] = 0
else: dp[j] = (dp[j])
dp[j] = (dp[j] + dp[j-1]) % MOD
return dp[-1]
tip
Attention to situations where the starting or ending point is a mine
Space complexity can be optimized with one-dimensional arrays
Large number operations must be added % MOD
Question 2 - Minimum Number of Umbrellas
Problem
Given the number of people needing shelter and a list of umbrella sizes, determine the minimum number of umbrellas required to cover exactly that number of people. If impossible, return -1.
Ideas
This is a variation of the minimum number of coins problem (complete backpack)
dp[t] denotes the minimum number of umbrellas required to bring up the number t
initialization dp[0]=0The rest are set to infinity.
Enumerate the size of each umbrella to update
Core code (Python)
def getUmbrellas(requirement, sizes).
INF = 10**9
dp = [INF] * (requirement + 1)
dp[0] = 0
for s in sizes: for t in range(s, requirement + 1)
dp[0] = 0 for t in range(s, requirement + 1): dp[0] = [INF] * (requirement + 1).
dp[t] = min(dp[t], dp[t-s] + 1)
return dp[requirement] if dp[requirement] < INF else -1
tip
Note that it's "full backpack" and not 0/1 backpack.
You can also use the BFS idea of shortest path, but DP is more direct.
If the requirement is large, DP time complexity should be watched (but the question gives a friendly upper bound)
Question 3 - Shortest Path to Collect All Coins
Problem
Chris must collect all gold coins in a maze and reach Alex. The maze is given as a 2D grid. 0=open, 1=blocked, 2=open+coin. Start at (0,0)Alex is at (x,y). Return the length of the shortest path to collect all coins and reach Alex, or -1 if impossible.
Ideas
Essentially "shortest way + must go through all gold coins"
Extend the state with BFS:(row, col, mask)where mask indicates which coins have been collected
Upon arriving at the (x,y) and the number of steps returned when the mask is full
Need three-dimensional visited arrays to avoid duplicates
Core code (Python)
from collections import deque
def minMoves(maze, ax, ay): n, m = len(maze), len(maze[0]).
n, m = len(maze), len(maze[0])
coins, cid = {}, 0
for i in range(n): for j in range(m): for j in range(m).
for j in range(m).
if maze[i][j] == 2.
coins[(i,j)] = cid
cid += 1
full_mask = (1 << cid) - 1
start_mask = (1 << coins[(0,0)]) if (0,0) in coins else 0
visited = [[[False]*(1<<cid) for _ in range(m)] for __ in range(n)]
q = deque([(0,0,start_mask,0)])
visited[0][0][start_mask] = True
dirs = [(1,0),(-1,0),(0,1),(0,-1)]
while q.
i,j,mask,d = q.popleft()
if i==ax and j==ay and mask==full_mask.
return d
for dx,dy in dirs.
ni,nj = i+dx, j+dy
if 0<=ni<n and 0<=nj<m and maze[ni][nj]! =1.
nmask = mask
if (ni,nj) in coins.
nmask |= (1<<coins[(ni,nj)])
if not visited[ni][nj][nmask]: (1<<coins[(ni,nj]]))
visited[ni][nj][nmask] = True
q.append((ni,nj,nmask,d+1))
return -1
tip
BFS statuses should be masked, otherwise there is no guarantee of gold collection
If you have a lot of gold, you can use the "Point-to-Point Shortest Circuit + TSP DP" solution instead.
Note that Alex's position may be a gold grid in itself, to be reflected in the start mask
This OA is one of the more common combinations on the HackerRank platform: a DP path counting, a full backpacking deformation, and a BFS state compression. The overall idea is not complicated, but there are a lot of details, especially the boundary and state representation. If you can familiarize yourself with these templates in advance, you can save a lot of debugging time in practice.
OA Preparation and Remote Assist Service
Many students prepare for the big factory OA, always stuck in the details or time management. In fact, there is no need to carry a person hard, we can help you do the whole remote assistance:
- OA real-time assists: HackerRank, CodeSignal, Cowboys.com Full Coverage
- Voice Alerts + No Trace Online: help you point out bugs at critical moments and ensure that the test is fully AC
- Interview assistance: VO, behavioral interviews, and system design all have real-time alerts
If you need it, feel free to contact us. The less you step in the pit, the more chance you have of making it to the next round.