Squirrel Nut Collection Shortest Path
Updated
Problem statement
A squirrel, a tree, and some nuts sit on a grid of cells. No two of them share a cell. The squirrel can carry only one nut at a time, and every nut must be brought to the tree.
The squirrel starts at squirrel. It walks to whichever nut it picks first and carries that nut to the tree. After that, every trip starts at the tree, goes to a nut, and comes back to the tree. Return the smallest total Manhattan distance needed to deliver all nuts.
Manhattan distance counts grid steps: take the difference between the two row values and the difference between the two column values, make both positive, and add them.
Function
minSquirrelDistance(tree: int[], squirrel: int[], nuts: int[][]) → intExamples
Example 1
tree = [2,2]squirrel = [4,4]nuts = [[3,0],[2,5]]return = 12Each nut is three steps from the tree. The squirrel is also three steps from the nut at [2,5], so it takes that nut first: three steps to the nut and three more to the tree. Then it makes a six-step round trip for the other nut. The total is twelve.
Example 2
tree = [0,0]squirrel = [2,2]nuts = [[1,0]]return = 4The squirrel walks three steps to the nut and one step from the nut to the tree. The total is four.
Constraints
1 <= nuts.length <= 50000.- Every row and column value is in
[0, 10000]. - The tree, the squirrel, and every nut are on different cells.
More Amazon Onsite Interview questions
- Assign Escalations to TeamsONSITE INTERVIEW · Seen Nov 2026
- Best Time to Buy and Sell StockONSITE INTERVIEW · Seen Nov 2026
- Course SchedulePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximum Earnings Across WarehousesONSITE INTERVIEW · Seen Nov 2026
- Priority Message BufferONSITE INTERVIEW · Seen Nov 2026
- Search in a Rotated Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Top K Frequent ElementsOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- LRU Cache for Query ResultsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Maximal SquareONSITE INTERVIEW · Seen Nov 2026
- Serialize and Deserialize a Binary TreeONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Maximize Two Endpoint ValuesGoogle · ONSITE INTERVIEWGoogle interview questions
- Minimum Rounds to Complete All TasksInfosys · OAInfosys interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMcKinsey & Company · OAMcKinsey & Company interview questions
- Maximize Revenue from Decreasing InventoryChubb · OAChubb interview questions
- Minimum Operations to Make Every Array Element OneOtter.ai · PHONE SCREENOtter.ai interview questions
- Minimum Values with Even Powers of TwoVisa · OAVisa interview questions
- Minimize Adjacent Squared-Difference Cost with One InsertionMicrosoft · OAMicrosoft interview questions
- Minimum Halvings for Alternating ParityIBM · OAIBM interview questions
- Resource AllocationMaven Securities · OAMaven Securities interview questions
- Rock JumpingIMC Trading · OAIMC Trading interview questions