The Handshakes That Don't Cross coding problem asks you to find the number of ways people sitting around a circular table can shake hands such that no two handshakes cross each other. Each person shakes hands with exactly one other person, so must be even. The result can be very large, so you need to return it modulo .
Amazon and other companies use this problem to test a candidate's knowledge of Combinatorics and Dynamic Programming. It is a classic application of Catalan Numbers. It evaluations your ability to break a circular problem into independent sub-problems by fixing one handshake and observing how it divides the remaining people into two isolated groups.
This problem follows the Dynamic Programming pattern.
dp[i] be the number of ways people can shake hands without crossing.dp[n] = sum(dp[j-2] * dp[n-j]) for all valid . This is the recursive formula for Catalan Numbers.people.
dp[0] * dp[2] = 1 * 1 = 1.dp[2] * dp[0] = 1 * 1 = 1.Familiarize yourself with the Catalan Number sequence (1, 1, 2, 5, 14...). It appears in many interview problems, such as "Counting valid parentheses," "Ways to triangulate a polygon," and "Number of unique Binary Search Trees." Recognizing the pattern early can save you significant time.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Digit Count in Range | Hard | Solve | |
| Number of Beautiful Integers in the Range | Hard | Solve | |
| Numbers With Repeated Digits | Hard | Solve | |
| Rotated Digits | Medium | Solve | |
| 2 Keys Keyboard | Medium | Solve |