Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Detect Duplicated Charges
00:00
5 left

Detect Duplicated Charges

MediumPython

Problem

Credit Genie receives a chronological stream of card charge events. A charge is potentially duplicated when the same account is charged the same merchant for the same amount and currency within a specified time window.

Implement detect_duplicate_charges, returning one pair for each duplicate event. For a duplicate at index i, pair it with the most recent earlier active event having the same charge signature.

Formal Specification

Input transactions is a list of dictionaries with keys timestamp, account_id, merchant_id, amount_cents, and currency. Timestamps are integer seconds and are nondecreasing. window_seconds is a nonnegative integer. Return a list of [previous_index, duplicate_index] pairs in stream order.

An earlier transaction remains active when previous_timestamp >= current_timestamp - window_seconds. Transactions outside the window cannot be paired. The current transaction is checked before it is added to the active window, so it cannot match itself.

Constraints

  • 1 <= len(transactions) <= 200000
  • 0 <= timestamp <= 10^12
  • 0 <= amount_cents <= 10^9
  • Each required string has at most 100 characters
  • Timestamps are nondecreasing
  • The time window includes events exactly window_seconds seconds before the current event

Function Signature

def detect_duplicate_charges(transactions, window_seconds):
Interviewer

Your question is Detect Duplicated Charges. Start with the requirements in the Question tab.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.