Top K Best Selling Products Per Store From Order Lines

Last updated: November 29, 2026

Quick Overview

Given order lines as (store_id, product_id, quantity), return for each store the k products with the most units sold, ties broken by product id ascending.

Instacart
Coding & Algorithms
Software Engineer
Instacart
November 29, 2026
Software Engineer
Online Assessment
Coding & Algorithms
Easy

0

0

0 solved


Given order lines as (store_id, product_id, quantity), return for each store the k products with the most units sold, ties broken by product id ascending.

A warm-up in the style of Instacart's online assessment, framed around the store-level bestseller shelves in the app. The grader is checking clean grouping, correct tie-breaking and awareness that a heap beats a full sort when k is small.

What the Interviewer Expects
  • Aggregate quantities with a dictionary of counters keyed by store
  • Apply a deterministic tie-break exactly as specified
  • Use heapq.nsmallest or a size-k heap to get O(m log k) per store
  • Handle stores with fewer than k products
  • Mention repeated lines for the same product must be summed first
Key Topics to Cover
hashing
heaps
top-k
aggregation
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 keep this current as orders stream in all day?
  • What if the counts only cover the last 7 days in a sliding window?
  • How would you do this across thousands of stores in SQL?
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: list of (store_id, product_id, qty). Output: dict of store to list of (product_id, units), at most k entries, sorted by units descending then product id ascending. Lines for the same produc...

High-Level Design

Two passes. First, aggregate units per (store, product) with a Counter per store. Second, for each store select the top k. Sorting each store's items is fine for small catalogs, but a store can carr...


Submit Your Answer
Markdown supported

Related Questions