MEDIUMasked at 1 company

Minimum Time to Visit All Houses

A medium-tier problem at 70% community acceptance, tagged with Array, Prefix Sum. Reported in interviews at Salesforce and 0 others.

Founder's read

Salesforce asks this problem, and it's a trap. Your first instinct is to simulate the visit order, but the array and prefix sum pattern hiding underneath makes that approach fail on larger inputs. The acceptance rate sits at 70%, which means 30% of candidates either TLE or get the logic wrong. This is one of those medium problems where the naive greedy move looks right until you hit a test case that proves it isn't. If you blank on the prefix sum trick during the live OA, StealthCoder surfaces the solution in seconds without the proctor seeing a thing.

Companies asking
1
Difficulty
MEDIUM
Acceptance
70%

Companies that ask "Minimum Time to Visit All Houses"

If this hits your live OA

Minimum Time to Visit All Houses is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know.

Get StealthCoder
What this means

The key insight is that you can't just visit houses in order or by distance. You need to recognize that some houses require backtracking, and the cost of backtracking is where prefix sums save you. The pattern: calculate the minimum time to visit all houses up to position i, then decide whether to go left-to-right or right-to-left from your current position. The cost of changing direction multiple times is what kills the greedy approach. Prefix sum lets you avoid recalculating cumulative costs every iteration. Most candidates miss that the problem is really asking for optimal direction switching, not just shortest path. When you're stuck mid-OA, StealthCoder breaks down the prefix sum structure and shows exactly which houses force a direction change.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Minimum Time to Visit All Houses recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Made by a working FAANG engineer who treats the OA the way companies treat hiring: as a game with rules you should know. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Time to Visit All Houses interview FAQ

Why doesn't greedy work on this problem?+

Greedy (always visit the closest unvisited house) forces unnecessary backtracking and revisits. The optimal solution sometimes takes a longer immediate path to avoid future costly reversals. Prefix sums let you precompute costs so you can compare direction strategies globally, not locally.

Is this problem still asked at Salesforce?+

Yes. Salesforce is the reported company in the data. It's a medium that rewards clean implementation and pattern recognition, not just brute force. It's the kind of problem that separates candidates who've seen the prefix sum trick from those who haven't.

What's the trick to using prefix sums here?+

Build a prefix sum of all house times up to each index. Then, when deciding whether to go right-to-left or left-to-right from a given position, use prefix sums to calculate total cost instantly instead of looping through subarrays. This cuts the time complexity from quadratic to linear.

How do I know when I need to change direction?+

You don't pre-decide. Instead, iterate through all possible split points and compare: cost of visiting left segment then right segment versus right segment then left. Prefix sums make this comparison O(1) per split point, not O(n). That's the win.

Will I have time to code this from scratch during an OA?+

If you've seen the pattern, yes. If you haven't, probably not. The acceptance rate of 70% suggests plenty of people get stuck on logic or optimization. If you hit a wall on this exact problem during your live assessment, StealthCoder gives you a working solution instantly.

Want the actual problem statement? View "Minimum Time to Visit All Houses" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.