Cross the most walls on a number line with limited energy
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in OpenAI's agentic coding (beta) round and at Scale AI, last asked Sep 2024. It tests two pointers, prefix sums and greedy.
Walls stand at distinct non-zero integer points on an infinite number line. Wall k is at positions[k] and costs costs[k] >= 0 energy to cross.
You start at 0 with energy units. Each step moves you one unit left or right and costs 1 energy. A wall at x > 0 is crossed every time you step between x - 1 and x, in either direction. A wall at x < 0 is crossed every time you step between x + 1 and x. Each crossing costs that wall's cost again, on top of the step. Your total spend can never go over energy, and you may stop anywhere.
Implement:
def max_walls(energy: int, positions: list[int], costs: list[int]) -> int:
...
Return the largest number of distinct walls you can cross.
Constraints
0 <= len(positions) == len(costs) <= 10^5-
-10^9 <= positions[k] <= 10^9,positions[k] != 0, all distinct -
0 <= costs[k] <= 10^9,0 <= energy <= 10^15
Example 1
energy = 10, positions = [3, -2, 6], costs = [2, 1, 1] → 2
Walk right to 6: 6 steps plus costs 2 + 1 = 9. Crossing -2 and 3 instead needs 11.
Example 2
energy = 15, same walls → 3
Walk left to -2 and back to 0 (2 * (2 + 1) = 6), then right to 6 (9). Total: 15.
Clarifying questions
- Does stopping exactly on a wall's position count as crossing it? By the rules a wall at
x > 0is crossed when you step ontoxfromx - 1; confirm. - Can the answer need the sum of costs past
2^63? Energy is at most10^15, but a sum of costs can be larger. - Is it ever useful to turn around more than once?
- A wall with cost 0 still costs the step to reach it; confirm.
What a strong answer covers
- The shape of an optimal walk: go to a farthest point on one side, come back through 0, then go to a farthest point on the other side; turning more than once only adds cost. Walls crossed are exactly those between the two turning points.
- On each side, the walls crossed are a prefix in order of distance from 0, so with walls sorted per side, the cost to cross the first
iwalls on a side is the distance to thei-th plus the prefix sum of their costs. - Trying both orders: the first side is paid twice, the second once. For each count on the doubled side, the most walls on the other side within the remaining energy by binary search (or a two-pointer, since costs are monotone),
O(n log n)overall. - Both examples checked: going right only (9 energy for 2 walls) versus left and back then right (15 for 3).
- Tests: no walls, walls on one side only, zero-cost walls, energy too small to reach any wall, large values that overflow 32-bit sums.
Follow-ups
- Return the walk itself, not only the count.
- Each wall gives a reward and you maximize the total reward instead.
- Walls only count when crossed in a given direction.
- Many queries with different energies: answer each in
O(log n).
Where it's asked
- OpenAI · Agentic coding (beta) · Last asked Sep 2024An existing codebase and changes too large to write by hand; you work through them with an AI coding agent · An AI coding agent is expected
- Scale AI
What it tests
two pointers · prefix sums · greedy
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.

