LeetCode Problem

How to Solve Invalid Transactions

Invalid transactions occur when a transaction exceeds $1000 or when the same person has two transactions within 60 minutes in different cities. The solution splits each transaction string into components and uses hash tables to track prior transactions per name for quick comparison. Iterating through all transactions ensures both amount-based and cross-city time conflicts are detected accurately.

GhostInterview Help

Need help with Invalid Transactions without spending extra time grinding it?

GhostInterview can read Invalid Transactions from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #1169Array scanning plus hash lookupReviewed 2026-03-07
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

Invalid transactions occur when a transaction exceeds $1000 or when the same person has two transactions within 60 minutes in different cities. The solution splits each transaction string into components and uses hash tables to track prior transactions per name for quick comparison. Iterating through all transactions ensures both amount-based and cross-city time conflicts are detected accurately.

Problem Statement

You are given an array of strings transactions where each string represents a transaction in the format "name,time,amount,city". A transaction is considered possibly invalid if the amount exceeds 1000 or if there is another transaction with the same name that occurs within 60 minutes in a different city.

Return an array of all possibly invalid transactions. Each transaction in the output should exactly match the input string, and the order of results does not matter.

Examples

Example 1

Input: transactions = ["alice,20,800,mtv","alice,50,100,beijing"]

Output: ["alice,20,800,mtv","alice,50,100,beijing"]

The first transaction is invalid because the second transaction occurs within a difference of 60 minutes, have the same name and is in a different city. Similarly the second one is invalid too.

Example 2

Input: transactions = ["alice,20,800,mtv","alice,50,1200,mtv"]

Output: ["alice,50,1200,mtv"]

Example details omitted.

Example 3

Input: transactions = ["alice,20,800,mtv","bob,50,1200,mtv"]

Output: ["bob,50,1200,mtv"]

Example details omitted.

Constraints

  • transactions.length <= 1000
  • Each transactions[i] takes the form "{name},{time},{amount},{city}"
  • Each {name} and {city} consist of lowercase English letters, and have lengths between 1 and 10.
  • Each {time} consist of digits, and represent an integer between 0 and 1000.
  • Each {amount} consist of digits, and represent an integer between 0 and 2000.

Solution Approach

Split and Parse Transactions

Convert each transaction string into a structured object with fields for name, time, amount, and city. This allows direct access for comparisons and prepares data for hash lookup and array scanning patterns.

Use Hash Table for Name-based Lookup

Maintain a hash table mapping each name to a list of previous transactions. For each new transaction, scan the hash table to detect time conflicts in different cities and simultaneously check the amount condition. This efficiently captures the core invalid transaction patterns.

Iterate and Collect Results

Traverse the entire transactions array and apply the validation rules. Append any transaction exceeding 1000 or conflicting in time and city to the result list. This ensures completeness without missing any cross-city conflicts within 60 minutes.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

Time complexity is O(n^2) in the worst case due to pairwise comparisons per name, but splitting and hash lookups reduce overhead. Space complexity is O(n) for storing transactions per name in hash tables.

What Interviewers Usually Probe

  • Checks if you can efficiently scan arrays while tracking past transactions with hash maps.
  • Wants you to correctly handle both amount threshold and cross-city time conflicts simultaneously.
  • May probe about optimizing nested loops or reducing redundant comparisons per user.

Common Pitfalls or Variants

Common pitfalls

  • Forgetting to compare transactions only with the same name, causing false positives.
  • Ignoring the 60-minute window or miscalculating the difference between transaction times.
  • Failing to preserve the original transaction string in the output while returning results.

Follow-up variants

  • Identify invalid transactions with a dynamic threshold instead of fixed 1000 amount.
  • Handle transactions across multiple cities with variable time windows.
  • Return transactions grouped by user instead of a flat list for further processing.

How GhostInterview Helps

  • Automatically splits transaction strings and organizes them for array scanning plus hash lookup validation.
  • Flags each invalid transaction with reasoning, highlighting both amount and time-city conflicts.
  • Provides step-by-step guidance on iterating, comparing, and collecting results efficiently.

Topic Pages

FAQ

What defines an invalid transaction in this problem?

A transaction is invalid if the amount exceeds 1000 or if another transaction with the same name occurs within 60 minutes in a different city.

How does the array scanning plus hash lookup pattern apply here?

Each transaction is scanned and compared against prior transactions stored in a hash table keyed by name, enabling quick detection of cross-city and time conflicts.

Can the output order differ from input?

Yes, the problem allows returning invalid transactions in any order as long as the strings match the original input.

What is the expected complexity?

Time complexity is worst-case O(n^2) per name due to pairwise checks, and space complexity is O(n) for storing transactions in hash tables.

How should ties within the 60-minute window be handled?

All transactions within 60 minutes across different cities for the same name should be marked invalid regardless of which appears first.

GhostInterview Solver

Need direct help with Invalid Transactions instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Invalid Transactions from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.