Detect a Circular Payment Chain Among Correspondent Banks
Last updated: November 29, 2026
Quick Overview
Given directed transfers between banks (payer to payee), return one cycle of banks that pay each other in a loop, as a list starting and ending with the same bank, or an empty list if no cycle exists.
J.P. Morgan
November 29, 20260
0
0 solved
Given directed transfers between banks (payer to payee), return one cycle of banks that pay each other in a loop, as a list starting and ending with the same bank, or an empty list if no cycle exists.
Circular flows between entities are a classic anti-money-laundering red flag, and J.P. Morgan's payments and compliance engineering teams use graph framing for onsite coding rounds. The interviewer wants a correct directed cycle detection (not the undirected version), a real cycle reconstruction, and awareness of recursion limits on large graphs.
What the Interviewer Expects
- Recognize that undirected visited-set logic gives false positives on directed graphs
- Use three-color DFS (unvisited, on stack, done) to find back edges
- Reconstruct the actual cycle with parent pointers
- Implement iteratively or explain the recursion depth risk
- State O(V + E) time and space
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you find all simple cycles up to length 4 for an investigator?
- How would you weight cycles so that only round trips moving most of the money are flagged?
- What if the transfer graph has 500 million edges and lives in a distributed store?
- How would you detect cycles incrementally as new transfers arrive?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: list of (payer, payee) edges, possibly with repeats and self-loops. Output: any one cycle like ['A','B','C','A'], or []. A self-transfer counts as a cycle of length one. Banks that only r...
High-Level Design
Directed cycle detection with three colors. WHITE means unvisited, GRAY means on the current DFS path, BLACK means fully explored. An edge to a GRAY node is a back edge, which proves a cycle. Edges to...