Lift-to-stairs switch with an energy budget
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Uber's online assessment, last asked Feb 2026. It tests prefix sums, math and simulation.
You start on floor 0 of a building with 0 energy and need to reach floor n. You ride the lift from floor 0 up to some floor k (0 <= k <= n), then walk the stairs from floor k to floor n.
-
Lift: each floor adds
e1energy and takest1time. Lift time isk * t1. -
Stairs: a floor climbed while your current energy is
Etakesceil(c / E)time, and then your energy drops bye2. Stair time is the sum over then - kstair floors. - Your energy may never drop below 0. A switch floor
kis valid only if you finish the stairs with energy>= 0, that isk * e1 - (n - k) * e2 >= 0.
Implement:
def min_time_difference(n: int, e1: int, t1: int, e2: int, c: int) -> int:
...
Return the minimum value of |lift time - stair time| over all valid switch floors k. Riding the lift all the way (k = n, no stairs) is always valid.
Constraints: 1 <= n, e1, t1, e2, c <= 10^5. An O(n^2) simulation is too slow for the largest inputs.
Example 1
n = 3, e1 = 2, t1 = 5, e2 = 1, c = 4 -> 1
-
k = 0: invalid (no energy). -
k = 1: lift time 5, energy 2. Stairs costceil(4/2) = 2(energy 1), thenceil(4/1) = 4(energy 0). Stair time 6, difference 1. -
k = 2: lift time 10, energy 4, one stair floor costs 1. Difference 9. -
k = 3: lift time 15, no stairs. Difference 15.
Example 2
n = 4, e1 = 3, t1 = 2, e2 = 2, c = 7 -> 0
Switch at k = 2: lift time 4, energy 6. Stairs cost ceil(7/6) = 2 (energy 4), then ceil(7/4) = 2 (energy 2). Stair time 4, difference 0. k = 1 is invalid: energy 3 cannot cover three stair floors at 2 each.
Clarifying questions
- When energy is exactly
0before a stair floor, is that floor impossible (division by zero), or does the validity rule already exclude it? - Is
k = 0always invalid, since you start with no energy? - If several
kgive the same minimum, is only the value returned? - Must the answer use exact integer
ceil, never floating point?
What a strong answer covers
- Writes the stair time for a switch at
k: the energies on the stairs arek*e1, k*e1 - e2, ..., one per stair floor, an arithmetic sequence with stepe2, and each costsceil(c / E). - Uses integer ceiling,
(c + E - 1) // E, and notes the validity rule keeps every energy on the stairs at leaste2, so it is never zero. - Gets past
O(n^2): any energyE >= ccosts exactly1, so only energies belowcneed real sums. Prefix sums ofceil(c / E)forE < c, kept per remainder mode2, give eachkinO(1)afterO(n + c)setup. - Evaluates every valid
k, since|lift - stairs|is not monotonic ink. - Checks the examples by hand and compares with a direct simulation on small random inputs.
Follow-ups
- Return the switch floor as well, with a tie rule.
- Allow switching back to the lift after some stairs.
- Energy also gains from rest stops at certain floors.
Where it's asked
- Uber · Online assessment · Last asked Feb 202690 min · Two problems on a HackerRank-based platform, camera and screen share on · No AI
What it tests
prefix sums · math · simulation
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.
