Infer a Store's Aisle Order From Shopper Walk Logs

Last updated: November 29, 2026

Quick Overview

Each shopper trip logs the sequence of aisles visited in a store, and every log respects one hidden global aisle order. Reconstruct a valid global order, or return an empty list if the logs contradict each other.

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

0

0

0 solved


Each shopper trip logs the sequence of aisles visited in a store, and every log respects one hidden global aisle order. Reconstruct a valid global order, or return an empty list if the logs contradict each other.

Instacart sorts a shopper's pick list by aisle so trips are efficient, and real stores rarely publish layouts. This onsite question checks whether you can model partial orderings as a graph, run a topological sort, and detect cycles caused by bad or conflicting data.

What the Interviewer Expects
  • Build edges only between consecutive aisles in each log, not all pairs
  • Deduplicate edges so in-degrees stay correct
  • Use Kahn's algorithm and detect a cycle when not every node is emitted
  • Make output deterministic, for example by first-seen order
  • Discuss what to do with noisy logs in production instead of failing outright
Key Topics to Cover
topological sort
Kahn's algorithm
cycle detection
directed graphs
data quality
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 tell whether the inferred order is unique?
  • Real logs are noisy: how would you find the order that violates the fewest observations?
  • How would you merge a retailer-provided partial layout with learned edges?
  • How does this change if a shopper can revisit an aisle within one trip?
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

Input: a list of walks, each a list of aisle names. If aisle A appears directly before B in any walk, A precedes B globally. Output any order consistent with all walks; [] if impossible. Repeated co...

High-Level Design

Every adjacent pair (u, v) in a walk is a directed edge u to v. Non-adjacent pairs are implied by transitivity, so adding them only wastes time. A valid order is a topological order of this graph, a...


Submit Your Answer
Markdown supported

Related Questions