In a triangular cost pyramid, why is filling from the bottom row upward simpler than from the apex?
answer
- count each cell's neighbours in both directions
- which direction has ragged edges
- where does the final answer sit
- two children below versus one parent above
- no final scan if you end at the apex
basics
~20 sFilling upward, every cell has exactly two children below it, so one uniform rule fills the whole pyramid and the answer lands at the apex. Filling downward needs edge-case handling and a final scan of the bottom row.
solid answer
~40 sIn a pyramid where row `i` holds `i+1` entries and a cell `(i, j)` may step to `(i+1, j)` or `(i+1, j+1)`, the two directions are not symmetric. Sweeping upward, `best[i][j] = cost[i][j] + min(best[i+1][j], best[i+1][j+1])` applies to every interior cell without exception, because every cell has exactly two children below it, and the answer ends up in the single apex cell. Sweeping downward, the leftmost and rightmost cell of each row have only one parent instead of two, so the transition needs branching at both edges, and the final answer is a minimum over the whole bottom row rather than a single lookup. Both directions are `O(n^2)` time, so this is a defensibility and boundary-bug argument, not a complexity one.
go deeper
Be ready to say which entries a cell can step to and to add up a five-row pyramid by hand. Knowing that the answer is a running total, not a per-row minimum, is the first thing checked.
Expect to state the upward recurrence with correct indices, explain why the downward one needs edge branches, and give the same time bound for both directions without claiming a speedup.
Show the reasoning that generalizes: pick the direction whose degree is uniform, and know that lazy evaluation wins when the reachable state space is sparse rather than dense.
The angle to own is when a hand-rolled sweep is worth its maintenance cost at all. A uniform recurrence with no boundary branches is the version a team can safely edit two years later.
## The shape A tiered surcharge pyramid has one entry in the top row, two in the next, three in the one after, and so on: row `i` holds `i+1` entries. From entry `j` of row `i` you may descend to entry `j` or entry `j+1` of row `i+1` — a step straight down or a step down-and-right. The task is the cheapest total from the apex to any entry of the base. It is a grid problem wearing a triangular hat, and the interesting content is that the two obvious fill directions are not equally pleasant. ## Downward: irregular in-edges Going top to bottom, define `best[i][j]` as the cheapest total from the apex down to `(i, j)`. Its predecessors are `(i-1, j-1)` and `(i-1, j)`. For a cell in the middle of a row both exist. For `j = 0` the first does not exist — the leftmost entry of every row can only be reached by descending straight down the left flank. For `j = i` the second does not exist, symmetrically. So the transition is three cases instead of one, and every case is a place to write an off-by-one. Worse, the answer is not in any single cell: you must scan the entire bottom row and take the minimum at the end. ## Upward: regular out-edges Go bottom to top instead and redefine `best[i][j]` as the cheapest total from `(i, j)` down to the base. Now the recurrence reads ``` best[i][j] = cost[i][j] + min(best[i+1][j], best[i+1][j+1]) ``` and it has no exceptions at all. Every cell that is not on the base has exactly two children, and `j+1` is always a valid index in the row below because that row is one entry wider. Seed the base row with its own costs and sweep upward; the answer is `best[0][0]`, a single lookup with no final scan. The asymmetry is structural, not stylistic: in this shape *out-degree is uniform and in-degree is not*. Choosing the direction with the uniform degree is what removes the boundary cases. That is the transferable lesson — when a DP has irregular in-edges, check whether reversing the direction gives you regular out-edges instead. ## Cost, and what does not change Both directions touch every one of the roughly `n^2 / 2` entries once and do constant work each, so both are `O(n^2)` time. Neither is asymptotically better. The upward sweep also compresses naturally to one row of scratch as wide as the base: keep an array holding the row below, and update in place with `scratch[j] = cost[i][j] + min(scratch[j], scratch[j+1])` sweeping `j` upward. Each entry is read before it is overwritten, so the in-place update is safe, and the extra space drops to `O(n)`. ## The claim not to overreach on "Bottom-up always beats top-down" is wrong as a general statement, and interviewers listen for it. Direction is chosen per problem shape. Recursion with memoization — logically the downward direction, evaluated lazily — wins whenever only a small fraction of the state space is actually reachable, because it computes only the states it needs, while a bottom-up sweep fills everything unconditionally. A dense pyramid needs every entry, so the sweep wins on constant factors and avoids recursion depth proportional to the number of rows, which is itself part of the space cost. On a sparse or heavily constrained state space the same reasoning points the other way. ## What an interviewer is checking Three things. First, that you noticed the in-degree asymmetry rather than defaulting to whichever direction you saw first. Second, that you can state the recurrence and the seed for your chosen direction without hand-waving the indices — `j` and `j+1` below, not `j-1` and `j`. Third, that you know the direction is a code-clarity and boundary-bug decision rather than a performance one, and you can say so plainly instead of claiming a speedup that is not there.
- Does the fill direction change the asymptotic cost?No. Both sweeps visit every entry once and do constant work each, so both are `O(n^2)` time on a pyramid of `n` rows, and both compress to `O(n)` extra space with a single row of scratch. The difference is entirely in boundary cases and in whether the answer is one lookup or a scan of the base row.
- When would lazy top-down evaluation still be the better choice here?When only a small part of the state space is reachable or the transitions are constrained, memoized recursion touches only the states it needs while a bottom-up sweep fills everything. On a dense pyramid every entry is required, so the sweep wins on constants and avoids recursion depth proportional to the row count, which counts as space.
- How do you compress the upward fill to a single row of scratch?Keep one array as wide as the base, initialized to the base costs. For each row above, sweep `j` from 0 upward writing `scratch[j] = cost[i][j] + min(scratch[j], scratch[j+1])`. Both reads happen before that index is overwritten and the entries beyond the current row's width are simply never touched again, so the update is safe in place.
Water running down a pyramid always has two channels ahead of it, but the outer stones have only one channel behind them. Tracing forward is uniform; tracing backward hits the ragged edge.
saying these in an interview costs you the question
- Claims bottom-up is always the correct DP direction
- Forgets edge entries have a single parent going downward
- Reads the answer from the apex after a downward fill
- Says the direction changes the asymptotic cost
- Uses j-1 and j as the children when sweeping upward