Problem Description
You are building a fraud detection feature for a mobile banking app. The bank tracks transaction sequences initiated by users. Each transaction may trigger subsequent transactions, forming a hierarchy of transactions.
Your task is to detect whether a sequence of transactions occurs more than once, i.e., if there is a duplicate transaction pattern in the system.
- A transaction pattern consists of a transaction and all subsequent transactions it triggers.
- Two patterns are considered duplicates if they have exactly the same sequence of transaction amounts and hierarchy.
- Only patterns consisting of two or more transactions are considered.
Example
Input:
arr = [10, 5, 10, 2, 5, -1, 5, -1, -1, 2, -1, -1, -1, -1, 2]
The pattern:
Transaction 5
└── Transaction 2appears twice, so a duplicate transaction sequence exists.
Function Description
Complete the function hasDuplicateTransactionPattern in the editor below.
Function Parameters:
int arr[n]: an array representing the transaction hierarchy in level-order traversal. Use -1 to denote a null (missing) transaction.
Returns:
boolean: Print true if there exists at least one duplicate transaction pattern of size ≥ 2, otherwise print false.
本题要求在银行交易形成的层级结构中,判断是否存在重复的交易模式。
输入使用二叉树的层序遍历表示交易关系,其中 -1 表示空节点。
只有满足以下条件,才算重复交易模式:
- 两个子树的交易金额及其层级结构完全相同。
- 每个模式至少包含两个有效交易节点。
- 只要存在一组重复模式,函数就返回
true,否则返回false。
考察知识点: Binary Tree、DFS、Tree Serialization、Hash Map、Duplicate Subtree Detection。
CSOAHELP 持续整理真实科技公司面试题,也设计贴近业务场景的练习题,提供 Mock Interview、系统设计辅导和实时面试思路支持。
我们也有代面试,面试辅助,OA代写等服务助您早日上岸~

