Squirrel Nut Collection Shortest Path
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
One squirrel, one tree, and a handful of nuts on a Manhattan grid. That's the whole setup in this Amazon OA reported in October 2026, and it looks like a graph problem until you read the example twice. It isn't. There's no search here, just distance math and one clever choice about which nut goes first. If you've got the assessment coming up in the next day or two, learn the trick and you can finish it in minutes. If you blank, StealthCoder runs invisibly on your screen as a safety net and hands you the solution while you type.
The problem
A squirrel, a tree, and several nuts occupy distinct cells on a rectangular Manhattan grid. The squirrel can carry one nut at a time. Every nut must be delivered to the tree. Examples Example 1 tree = [2,2] squirrel = [4,4] nuts = [[3,0],[2,5]] return = 12 Taking the nut at [2,5] first avoids any extra distance versus a tree round trip.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that every nut except the first costs a fixed round trip: 2 times the Manhattan distance from nut to tree. The squirrel starts at its own cell, so only the first nut changes the total. Sum 2*dist(nut, tree) over all nuts, then for each nut compute the savings: dist(nut, tree) - dist(squirrel, nut). The first leg goes squirrel to nut, then nut to tree, instead of a full tree round trip. Pick the nut with the maximum savings and subtract it from the total. Common pitfall: running BFS or Dijkstra, or trying permutations. The grid is open, so Manhattan distance is exact. Another pitfall is picking the nut closest to the squirrel instead of maximizing savings. In the example, the answer 12 comes from choosing [2,5] first. StealthCoder is your hedge if the formula slips your mind mid-assessment. The code is about ten lines and O(n).
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill Squirrel Nut Collection Shortest Path 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as squirrel simulation. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Squirrel Nut Collection Shortest Path FAQ
How hard is the squirrel nut collection problem really?+
Easy once you see it, annoying if you don't. The code is a single loop. The difficulty is realizing it's not a pathfinding problem. Candidates who reach for BFS waste time. Spot the fixed round-trip cost and the answer falls out quickly.
What's the trick to solving it?+
Total cost is 2 times the sum of each nut's distance to the tree, minus the best savings from choosing one nut first. Savings for a nut is its tree distance minus the squirrel's distance to it. Maximize that value, subtract once.
Do I need BFS or Dijkstra for the grid?+
No. The grid has no obstacles mentioned, and movement is Manhattan. Distance between two cells is |r1-r2| + |c1-c2|. Building a graph or running a search adds code and risk without changing the answer.
What edge cases should I test?+
Test a single nut, where the answer is just squirrel to nut plus nut to tree. Test a nut sitting between the squirrel and the tree, where savings can be large. Also check that savings can be negative, so initialize your max carefully, not at zero.
How do I prepare for this in 48 hours?+
Write the solution once from memory with the savings formula, then run the example and confirm you get 12. Then do two or three other Manhattan distance problems. The pattern is short enough that one clean pass is real preparation.