Find suspicious users involved in at least a threshold number of transactions

Quick Overview

Given transaction logs in the form sender, receiver, amount and a threshold, return every user involved in at least that many transactions, sorted by numeric ID. Tests hash map counting, the rule that a self-transaction counts once, and sorting string IDs by their numeric value.

Find suspicious users involved in at least a threshold number of transactions

Company: Snowflake

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given a list of transaction `logs`, where each log is a string in the format `"sender_id receiver_id amount"`. You are also given an integer `threshold`. A user is considered suspicious if they have been involved in at least `threshold` transactions. A user is involved in a transaction if they are the sender or the receiver. If the sender and receiver are the same user, that transaction counts as only one involvement for that user. Return a list of the IDs (as strings) of all suspicious users, sorted in ascending order by their numeric value. ### Function Signature ```python def find_suspicious_users(logs: list[str], threshold: int) -> list[str]: ... ``` ### Rules - Each log has exactly three fields separated by single spaces: the sender ID, the receiver ID and the amount. The amount has no effect on the answer. - IDs are non-negative decimal integers written without leading zeros, so each user has exactly one string form. - Every log counts, including repeated transactions between the same two users. - Return each suspicious ID once, in the string form it has in the logs, ordered by numeric value (so `"9"` comes before `"10"`). Return an empty list when no user reaches the threshold. ### Constraints - `1 <= len(logs) <= 10^5` - `0 <= sender_id, receiver_id <= 10^9` - `1 <= amount <= 10^9` - `1 <= threshold <= len(logs)` ### Examples ```text Input: logs = ["1 2 100", "3 1 50", "1 3 200", "2 3 150"], threshold = 2 Output: ["1", "2", "3"] ``` User 1 appears in logs 0, 1 and 2 (three involvements), user 2 in logs 0 and 3 (two), and user 3 in logs 1, 2 and 3 (three). All three reach the threshold of 2. ```text Input: logs = ["1 1 100", "2 2 200", "3 3 300"], threshold = 2 Output: [] ``` Every log is a self-transaction, so each user has exactly one involvement and nobody reaches 2. ```text Input: logs = ["10 9 5", "9 10 7", "2 10 1"], threshold = 2 Output: ["9", "10"] ``` User 10 is in all three logs, user 9 is in the first two, and user 2 is only in the last one. Users 9 and 10 qualify, and numeric order puts 9 before 10.

Overview: Given transaction logs in the form sender, receiver, amount and a threshold, return every user involved in at least that many transactions, sorted by numeric ID. Tests hash map counting, the rule that a self-transaction counts once, and sorting string IDs by their numeric value.

Snowflake logo
Snowflake
Nov 25, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given a list of transaction logs, where each log is a string in the format "sender_id receiver_id amount". You are also given an integer threshold.

A user is considered suspicious if they have been involved in at least threshold transactions. A user is involved in a transaction if they are the sender or the receiver. If the sender and receiver are the same user, that transaction counts as only one involvement for that user.

Return a list of the IDs (as strings) of all suspicious users, sorted in ascending order by their numeric value.

Function Signature

def find_suspicious_users(logs: list[str], threshold: int) -> list[str]:
    ...

Rules

  • Each log has exactly three fields separated by single spaces: the sender ID, the receiver ID and the amount. The amount has no effect on the answer.
  • IDs are non-negative decimal integers written without leading zeros, so each user has exactly one string form.
  • Every log counts, including repeated transactions between the same two users.
  • Return each suspicious ID once, in the string form it has in the logs, ordered by numeric value (so "9" comes before "10" ). Return an empty list when no user reaches the threshold.

Constraints

  • 1 <= len(logs) <= 10^5
  • 0 <= sender_id, receiver_id <= 10^9
  • 1 <= amount <= 10^9
  • 1 <= threshold <= len(logs)

Examples

Input: logs = ["1 2 100", "3 1 50", "1 3 200", "2 3 150"], threshold = 2
Output: ["1", "2", "3"]

User 1 appears in logs 0, 1 and 2 (three involvements), user 2 in logs 0 and 3 (two), and user 3 in logs 1, 2 and 3 (three). All three reach the threshold of 2.

Input: logs = ["1 1 100", "2 2 200", "3 3 300"], threshold = 2
Output: []

Every log is a self-transaction, so each user has exactly one involvement and nobody reaches 2.

Input: logs = ["10 9 5", "9 10 7", "2 10 1"], threshold = 2
Output: ["9", "10"]

User 10 is in all three logs, user 9 is in the first two, and user 2 is only in the last one. Users 9 and 10 qualify, and numeric order puts 9 before 10.