Max Energy
Reported by candidates from Flexport's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Flexport hit you with a grid traversal problem in February 2024, and it's a path optimization question disguised as energy management. You start at the top of a 4x4 matrix with 100 energy, move downward, and every cell costs you energy equal to its value. The trick is that you're not maximizing a score at the end, you're maximizing what's left in the tank. This is dynamic programming, and the catch is that negative energy is allowed, which changes how you think about the problem.
The problem
Given a 4 x 4 matrix mat, the initial energy is 100. The task is to reach the last row of the matrix with the maximum possible energy left. After stepping on a cell (i, j), energy decreases by mat[i][j] units. Find the maximum possible energy left at the end of the traversal. Note: The final energy can be negative.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern here is dynamic programming on a grid. You need to track the maximum energy possible at each cell as you descend row by row. At each position (i, j), your energy is the best energy from the previous row minus the cost of landing on (i, j). The key insight: you can move to any cell in the next row, not just adjacent ones, unless the problem restricts movement (re-read for this). Build a DP table where dp[i][j] = max energy you can have when you reach cell (i, j). Fill it row by row from top to bottom. The answer is the maximum value in row 3 (the last row). StealthCoder will catch if you misread the movement rules or forget to initialize the first row correctly.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Max Energy cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as minimum path sum. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Flexport's OA.
Flexport reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Max Energy FAQ
Can I move diagonally or only straight down?+
Re-read the problem statement carefully. Flexport's version doesn't specify, so assume the most common case: you can move to any adjacent cell (left, right, down, or down-diagonal). If it's unclear during the OA, clarify with the proctor or code the most flexible version first.
Why does negative energy matter here?+
Because you're trying to maximize energy left, not minimize cost. A cell with value 200 costs you 200, but you might have only 50 energy. Going negative is allowed, so sometimes the 'best' path is the one that lets you survive the longest overall, not the path with highest values.
Do I need to use memoization or can I do bottom-up DP?+
Bottom-up DP is cleaner here. Build a 4x4 table, fill the first row with 100 minus each cell's cost, then for each subsequent row, compute the max energy reachable from any cell in the prior row. No recursion needed.
What's the time and space complexity?+
Time is O(n^2 * m) where n is rows and m is columns (4x4 is small, so this doesn't matter). Space is O(n * m) for the DP table, or O(m) if you optimize to one row at a time. Flexport won't penalize either.
How do I test if my solution is right?+
Trace a simple 2x2 example by hand first. Verify that the DP table fills correctly and that the final answer is the max of the last row. Check edge cases: all high-cost cells, negative final energy, and a cell with cost 0.