Expected and Simulated Shoelace Loops
Updated
Problem statement
You have n shoelaces. Each one is a string with two ends. String i has the end labels 2*i and 2*i+1, for 0 <= i < n.
In the random experiment, pick two free ends uniformly at random and tie them together. Repeat until no free ends are left. The strings then form closed loops. Tying a string's two ends to each other makes one loop.
Return two values in this order:
- The theoretical expected number of loops in this experiment. Work it out exactly with math. The average of a finite simulation does not count.
- The empirical mean: the average number of loops over the rows of
trials. Really replay the ties in each row, count its loops, and average those counts.
Each row of trials lists every end label from 0 through 2*n-1 exactly once, in some order (a permutation). In a row, tie the ends at positions 0 and 1, then 2 and 3, and so on. Each row starts again from the original n untied strings.
The rows give you the pairings, not their loop counts. Replay them as given, and do not draw any new random values. Rows made from independent uniform random permutations would match the random experiment. But any valid row can appear here, and rows may repeat. So the empirical mean does not have to equal the theoretical expectation, or even be close to it.
When n = 0, every row is empty and both results are 0. Do not change trials. Each call works on its own and returns a new two-value array.
Small rounding errors are fine. A returned value answer is accepted when |answer - exact| <= 0.000001 * max(1, |exact|). Here exact is the true theoretical expectation or the true mean of the given trials.
Function
shoelaceLoopStats(n: int, trials: int[][]) → double[]Examples
Example 1
n = 2trials = [[0,1,2,3],[0,2,1,3],[0,3,1,2]]return = [1.3333333333333333,1.3333333333333333]For two strings, the theoretical expected loop count is 1 + 1/3 = 4/3.
The first row ties each string's own ends, making 2 loops. Each of the other rows joins both strings into 1 loop. The empirical mean is (2 + 1 + 1)/3 = 4/3, so both returned values are about 1.3333333333.
Example 2
n = 3trials = [[0,2,1,4,3,5],[0,1,2,3,4,5]]return = [1.5333333333333334,2.0]The theoretical expectation for three strings is 1 + 1/3 + 1/5 = 23/15, about 1.5333333333.
The first row joins all three strings into 1 loop. The second row ties each string's own ends, making 3 loops. The empirical mean of these two rows is (1 + 3)/2 = 2. So the two returned values are different.
Example 3
n = 0trials = [[],[]]return = [0.0,0.0]There are no strings or ends to connect. Each of the two empty trials makes 0 loops. Both the theoretical expectation and the empirical mean are 0.
Constraints
0 <= n <= 100.1 <= trials.length <= 20.- Each trial row contains exactly
2*nlabels, so its length is between 0 and 200, inclusive. - Every endpoint label is an integer between 0 and 199, inclusive.
- Every trial row is a permutation of the labels
0through2*n-1. Whenn = 0, that permutation is empty.
More Akuna Capital Phone Screen questions
- Match Orders by Price and Arrival TimeOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Biased Coin: Probability of Exactly h HeadsPHONE SCREEN · Seen Nov 2026
- Positive Nth Root with Gradient DescentPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Two SumOA · PHONE SCREEN · Seen Sep 2026
- Maximum Value in a Binary TreePHONE SCREEN · Seen Sep 2026
- Tiered Expiring Item StorePHONE SCREEN · Seen Aug 2026
- Statistics for the Latest K ValuesPHONE SCREEN · Seen Nov 2025
- Remove Duplicates While Preserving OrderPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2025
- Sliding Window MaximumPHONE SCREEN · Seen Oct 2025
- Evaluate a PolynomialPHONE SCREEN · Seen Oct 2025
Similar questions at other companies
- Sliding Window Attention over a KV CacheNVIDIA · PHONE SCREENNVIDIA interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN
- Monthly Calendar with Custom Week StartStripe · PHONE SCREENStripe interview questions
- Fractional Share Inventory FlatteningRobinhood · PHONE SCREEN · ONSITE INTERVIEWRobinhood interview questions
- Logistic Regression DiagnosticsLinkedIn · PHONE SCREENLinkedIn interview questions
- Evenly Schedule Vehicle InspectionsAmazon · PHONE SCREEN
- Implement Cross-Entropy LossOpenAI · PHONE SCREENOpenAI interview questions
- Cumulative No-Intercept Regression BetasCitadel · PHONE SCREENCitadel interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Per-Segment SpeedingOnePay · PHONE SCREENOnePay interview questions
Akuna Capital interview experiences
- Akuna Capital Quant Trader and QR Interviews: 5 Experiences
- Akuna Capital JQR CodePair Rounds: 8 Experiences (Part 6 of 6)
- Akuna Capital QR Probability Phones: 14 Experiences (Part 5 of 6)
- Akuna Capital QR-ML Interviews: 11 Experiences (Part 4 of 6)
- Akuna Capital QR Regression Rounds: 11 Experiences (Part 3 of 6)
- Akuna Capital QR Coding Interviews: 17 Experiences (Part 2 of 6)