← Meta Interview Insights

Meta·Machine Learning Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026

Summary

Three coding questions for a Meta MLE role. Nothing behavioral, just back-to-back algorithms. The problems were harder than I expected for a phone screen, especially the first one.

Questions Asked (3)

Q1

Given an array of integers that may have duplicates, return the second-largest distinct permutation in lexicographic order.

Algorithms & Data Structures
Author's notes

This one took me a minute to even parse.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem: 'second-largest distinct permutation in lexicographic order' likely means the permutation that is immediately previous to the largest permutation when all distinct permutations are sorted lexicographically. Then, derive an algorithm by adapting the standard next permutation method to find the previous permutation, handling duplicates by skipping identical adjacent elements.

Pro tip: After presenting your solution, discuss how it can be optimized for large arrays with many duplicates, and mention that the same logic can be extended to find the k-th permutation, showing depth beyond the immediate question.

1. Clarify the problem

Confirm that 'second-largest distinct permutation' means the permutation immediately preceding the lexicographically largest permutation among all distinct permutations. Ask if the input array can be modified and if the output should be a list or array.

2. Identify the largest permutation

The largest permutation is the array sorted in descending order. The second-largest is the previous permutation in lexicographic order.

3. Develop the previous permutation algorithm

Adapt the standard next permutation algorithm: find the rightmost index i where arr[i] > arr[i+1], then find the largest index j > i with arr[j] < arr[i], swap them, and reverse the suffix after i to be in descending order.

4. Handle duplicates and edge cases

Ensure that duplicates are handled correctly by skipping equal elements when searching for i and j. If no such i exists, the array is already the smallest permutation, so there is no second-largest; return an empty array or appropriate error.

5. Analyze complexity and test

The algorithm runs in O(n) time and O(1) extra space. Test with arrays containing duplicates, already sorted ascending/descending, and single-element arrays.

Key Points to Mention

  • Lexicographic order and distinct permutations
  • Previous permutation algorithm (reverse of next permutation)
  • Handling duplicates by skipping equal elements
  • Time and space complexity: O(n) time, O(1) space
  • Edge cases: no second-largest permutation exists
  • Relation to next permutation and k-th permutation problems

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

Q2

You have m sorted arrays. Return the first k elements in globally sorted order, preserving duplicates.

Algorithms & Data Structures
Author's notes

Classic heap problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a min-heap to merge the m sorted arrays by initially pushing the first element of each array along with its array index and element index. Then repeatedly pop the smallest element, append it to the result, and push the next element from the same array until k elements are collected or the heap is empty.

Pro tip: Discuss the time and space complexity trade-offs: the heap approach is O(k log m) time and O(m) space, which is optimal for large m and small k. Also mention edge cases like empty arrays, k larger than total elements, and duplicate handling.

1. Clarify the problem and constraints

Ask about the size of m, k, and the arrays, whether k can exceed total elements, and if the arrays are sorted in ascending order. Confirm that duplicates should be preserved.

2. Choose the right data structure

Select a min-heap to efficiently track the smallest current element among the m arrays. Each heap entry should store the value, the array index, and the element index within that array.

3. Initialize the heap

Push the first element of each non-empty array into the heap. If an array is empty, skip it. This takes O(m) time.

4. Extract and refill

While the heap is not empty and we have collected fewer than k elements, pop the minimum, add it to the result, and if the popped element has a next element in its array, push that next element into the heap.

5. Analyze complexity and edge cases

State that time complexity is O(k log m) and space is O(m) for the heap plus O(k) for the output. Handle edge cases: k=0, empty arrays, k > total elements, and duplicate values.

Key Points to Mention

  • Min-heap (priority queue) for efficient merging
  • Storing array index and element index in heap nodes to know where to fetch the next element
  • Time complexity O(k log m) and space complexity O(m + k)
  • Handling duplicates by simply preserving the order of pops
  • Edge cases: empty arrays, k larger than total elements, k=0
  • Alternative approaches like binary search on value range or divide-and-conquer, but heap is optimal for this scenario

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

Q3

Given a list of words, find a subset whose concatenation uses each character at most once, maximizing the number of unique characters. How does your approach scale to thousands of words?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The bitmask DP angle is the clean solution here since there are only 26 letters.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model each word as a 26-bit mask indicating which characters it contains, then use dynamic programming over subsets of characters to find the maximum number of unique characters achievable by concatenating a subset of words. For scaling to thousands of words, prune words with duplicate characters and use meet-in-the-middle or branch-and-bound with bitmask operations to handle the exponential search space efficiently.

Pro tip: Emphasize that the problem is NP-hard (related to set packing), so for large inputs you must discuss approximation or heuristic approaches, and mention that Meta values pragmatic trade-offs between optimality and scalability.

1. Clarify constraints and assumptions

Ask about word length, alphabet size, and whether words can be used multiple times. Confirm that the goal is to maximize unique characters, not the number of words.

2. Preprocess words into bitmasks

For each word, compute a 26-bit integer where bit i is set if the i-th letter appears. Discard words with duplicate characters (mask has fewer bits than word length) and deduplicate masks.

3. Formulate as a set packing problem

The task reduces to selecting a set of masks with no overlapping bits, maximizing the total number of set bits. This is equivalent to maximum weight set packing on a universe of 26 elements.

4. Design an algorithm for small to medium inputs

Use dynamic programming over character subsets: dp[mask] = max unique characters achievable using a subset of words whose combined mask is exactly mask. Iterate over words and update dp[mask | word_mask] if disjoint.

5. Scale to thousands of words

Apply pruning: remove dominated masks (if mask A is subset of mask B, A is never better). Use meet-in-the-middle: split words into two halves, enumerate all valid combinations for each half, then combine. Alternatively, use branch-and-bound with upper bounds based on remaining characters.

Key Points to Mention

  • Bitmask representation for efficient set operations (AND, OR, popcount).
  • NP-hardness of the problem (set packing) and implications for exact solutions.
  • Dynamic programming over 2^26 states is infeasible; need pruning or alternative methods.
  • Meet-in-the-middle reduces time complexity from O(2^N) to O(2^(N/2)).
  • Dominated mask elimination: if mask A is a subset of mask B, A can be discarded.
  • Trade-offs between exact and approximate solutions for large-scale inputs.

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