Flag Cards Paying Too Many Merchants in a Window

Last updated: November 29, 2026

Quick Overview

Given a time-ordered stream of card payments (timestamp, card fingerprint, merchant id), flag each payment where that card has paid more than k distinct merchants within the trailing window of T seconds.

Square
Coding & Algorithms
Software Engineer
Square
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a time-ordered stream of card payments (timestamp, card fingerprint, merchant id), flag each payment where that card has paid more than k distinct merchants within the trailing window of T seconds.

Card testing, where a stolen card is tried at many small sellers quickly, is a classic risk signal for a processor serving millions of sellers like Square. The interviewer checks sliding window mechanics with distinct counting, precise window boundaries, and how you would bound memory in a streaming setting.

What the Interviewer Expects
  • Use a per-card deque plus a merchant counter
  • Define the window boundary precisely (half-open interval)
  • Achieve amortized O(1) per event
  • Discuss memory cleanup for idle cards
  • Explain how it would run partitioned by card in a stream processor
Key Topics to Cover
sliding window
hash maps
stream processing
fraud signals
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 do you evict state for cards that never come back?
  • Events can arrive up to 5 seconds late; what changes?
  • How would you make k depend on the merchant category?
  • How would you run this across many machines?
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 sorted by timestamp. Window is (ts - T, ts]. Return indices of payments that cause the distinct-merchant count to exceed k. Repeat payments at the same merchant do not raise the count....

High-Level Design

For each card keep a deque of (ts, merchant) in the window and a Counter of merchant occurrences. On each event, pop from the left while the oldest is outside the window, decrementing and deleting zer...


Submit Your Answer
Markdown supported

Related Questions