Group Amazon Search Reformulations Into Chains and Measure Conversion

Last updated: November 29, 2026

Quick Overview

Given search and purchase events per session, group consecutive searches no more than two minutes apart into reformulation chains, then report how many chains ended in a purchase and the average searches per chain.

Amazon
Data Manipulation (SQL/Python)
Software Engineer
Amazon
November 29, 2026
Software Engineer
Technical Screen
Data Manipulation (SQL/Python)
Medium

0

0

0 solved


Given search and purchase events per session, group consecutive searches no more than two minutes apart into reformulation chains, then report how many chains ended in a purchase and the average searches per chain.

A Python data manipulation question in the style of Amazon Search teams, where a customer rewording a query repeatedly is a signal that results were poor. The interviewer checks per-session grouping, gap-based chaining, and a clear rule for attributing a purchase to a chain.

What the Interviewer Expects
  • Group events by session and sort by timestamp
  • Start a new chain when the gap since the previous search exceeds the threshold
  • Attribute a purchase to the most recent open chain and ignore purchases before any search
  • Break timestamp ties so a search and purchase at the same second are ordered sensibly
  • Return clear aggregate metrics and handle empty input
Key Topics to Cover
sessionization
grouping
gap threshold
search analytics
Python
How to Approach This
  1. Clarify the schema and expected output format before writing queries.
  2. Use CTEs (WITH clauses) to break complex queries into readable steps.
  3. Consider window functions (ROW_NUMBER, RANK, LAG, LEAD) for ranking and sequential analysis.
  4. Watch for NULLs, duplicates, and edge cases in JOINs and GROUP BY.
  5. For pandas, prefer vectorized operations over row-by-row iteration.
Possible Follow-up Questions
  • How would you require that consecutive queries share a token to count as a reformulation?
  • How would you report conversion by chain length?
  • How would you do this in SQL with LAG and a running sum?
  • What would you change if events arrive out of order in a stream?
Sharpen Your Skills on Codemia

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

Practice SQL Problems
Sample Answer
Clarify the Data

Input: a list of dicts with session_id, ts in epoch seconds, and type of search or purchase. Definitions to confirm:

  • A chain is a run of searches in one session where each search is at mos...
Approach

Sort each session's events by (ts, type != 'search') so a search and a purchase logged in the same second process the search first. Walk the events keeping the current chain and the last search time...


Submit Your Answer
Markdown supported