← Turing Interview Insights

Turing·Software Engineer·Online Assessment (OA)·Intermediate

Intermediate
Jul 2026Remote

Summary

Got an online assessment from Turing for a software engineer role, and it was basically one algorithmic problem about counting permutations with a specific number of inversions. Pretty math-heavy for what I expected.

Questions Asked (1)

Q1

Given two integers m and n, count the number of permutations of the array [1, 2, ..., m] that contain exactly n inversions. Return the result modulo 10^9 + 7.

Algorithms & Data Structures
Author's notes

This took me a minute to even parse.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use dynamic programming where dp[i][j] represents the number of permutations of length i with exactly j inversions. The recurrence is dp[i][j] = sum_{k=0}^{min(j, i-1)} dp[i-1][j-k], which can be optimized with prefix sums to O(m*n) time and O(n) space. Return dp[m][n] modulo 10^9+7.

Pro tip: Mention that the maximum number of inversions for length m is m*(m-1)/2, so if n exceeds this, return 0 immediately. Also, use a sliding window to compute the prefix sums efficiently, reducing the time complexity from O(m*n^2) to O(m*n).

1. Define the DP state

Let dp[i][j] be the number of permutations of length i with exactly j inversions. Initialize dp[0][0] = 1 and all other entries to 0.

2. Derive the recurrence

When inserting the largest element i into a permutation of length i-1, it can create between 0 and i-1 new inversions. So dp[i][j] = sum_{k=0}^{min(j, i-1)} dp[i-1][j-k].

3. Optimize with prefix sums

Maintain a running sum of the last i values of dp[i-1] to compute each dp[i][j] in O(1) time, reducing the overall time complexity to O(m*n).

4. Handle edge cases and modulo

If n > m*(m-1)/2, return 0. Perform all additions modulo 10^9+7 to prevent overflow.

5. Return the result

After filling the DP table up to m and n, return dp[m][n] modulo 10^9+7.

Key Points to Mention

  • Dynamic programming state definition and recurrence relation.
  • Maximum possible inversions for length m is m*(m-1)/2, so early exit if n exceeds this.
  • Optimization using prefix sums or sliding window to achieve O(m*n) time complexity.
  • Space optimization to use only O(n) space by keeping only the previous row.
  • Modulo arithmetic to handle large numbers (mod 10^9+7).
  • Time and space complexity analysis: O(m*n) time, O(n) space.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.