Fewest Steps to Pick Every Item and Reach Checkout

Last updated: November 29, 2026

Quick Overview

A store is a grid with walls (shelves), a start cell, a checkout cell and up to 10 cells holding items on the customer's list. Return the fewest moves a shopper needs to pick every item and end at checkout, or -1 if impossible.

Instacart
Coding & Algorithms
Software Engineer
Instacart
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Hard

0

0

0 solved


A store is a grid with walls (shelves), a start cell, a checkout cell and up to 10 cells holding items on the customer's list. Return the fewest moves a shopper needs to pick every item and end at checkout, or -1 if impossible.

This Instacart onsite problem mirrors shopper pick-path optimization. Interviewers want BFS over an expanded state space (position plus collected set), with a bitmask, and a clear explanation of why plain BFS on cells fails.

What the Interviewer Expects
  • Explain why state must include which items have been collected
  • Encode collected items in a bitmask and BFS over (row, col, mask)
  • Pick up an item when stepping onto its cell, including the start cell
  • Return as soon as the checkout is reached with a full mask
  • Analyze O(R * C * 2^K) and discuss when K is too large
Key Topics to Cover
BFS
bitmask state
grid graphs
shortest path
state space search
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you solve it with K = 30 items?
  • How would you precompute pairwise distances and switch to a TSP DP over items?
  • What if some items have two possible locations in the store?
  • How would you make walking past the frozen aisle last a constraint?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Grid of strings with S, E, #, . and lowercase letters for items. Moves are 4-directional, each costs 1. Items are picked by stepping on them, no extra cost. Must collect all distinct item lett...

High-Level Design

BFS on cells alone is wrong because the same cell is worth revisiting after collecting more items. So the node is (r, c, mask) where bit i is set once item i is collected. All edges have weight 1, s...


Submit Your Answer
Markdown supported

Related Questions