← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Meta SWE coding round, one problem the whole session. The problem looked like a permutation puzzle but had enough edge cases to trip you up if you weren't careful about duplicates.

Questions Asked (1)

Q1

Given an integer array that may contain duplicates, return the second largest distinct permutation (lexicographically) that can be formed from its elements.

Algorithms & Data Structures
Author's notes

My first instinct was to just generate the largest permutation, then do a next-permutation-in-reverse kind of thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that 'second largest distinct permutation' means the permutation that is immediately smaller than the lexicographically largest distinct permutation. Then, sort the array in descending order to get the largest permutation, and apply the previous permutation algorithm (next permutation in reverse) to find the second largest distinct permutation, handling duplicates by skipping equal adjacent elements.

Pro tip: Mention that duplicates can be handled by treating the array as a multiset and using the same algorithm as next permutation but with reversed comparisons; also note that if no second largest exists (e.g., all elements identical), return an empty array or a sentinel value.

1. Clarify the problem

Confirm that 'second largest distinct permutation' means the permutation that is immediately smaller than the lexicographically largest distinct permutation. Ask about edge cases like arrays with fewer than two distinct permutations.

2. Generate the largest permutation

Sort the array in descending order to obtain the lexicographically largest permutation. This serves as the starting point.

3. Find the previous permutation

Implement the previous permutation algorithm: from right to left, find the first index i where arr[i] > arr[i+1]. Then find the largest element to the right of i that is smaller than arr[i], swap them, and reverse the subarray after i to get the next smaller permutation.

4. Handle duplicates and edge cases

Ensure the algorithm works with duplicates by skipping equal elements when searching for the swap candidate. If no such i exists, there is no smaller permutation, so return an empty array or appropriate indicator.

5. Return the result

Return the modified array as the second largest distinct permutation. Discuss time complexity: O(n log n) for sorting plus O(n) for the previous permutation step.

Key Points to Mention

  • Lexicographic order and permutation generation
  • Previous permutation algorithm (mirror of next permutation)
  • Handling duplicates in permutations
  • Time and space complexity analysis
  • Edge cases: all elements identical, array size < 2, no second largest permutation
  • In-place modification vs. creating a new array

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