← Meta Interview Insights

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

Intermediate
May 2026

Summary

Meta SWE coding round with one algorithmic problem about sorting and minimum moves. Not a lot of context in the original post but the problem itself was interesting enough to think about.

Questions Asked (1)

Q1

Given an unsorted array like [5, 2, 2, 4, 3], what is the minimum number of move operations needed to transform it into a sorted array, where each operation moves one element from its current position to another?

Algorithms & Data Structures
Author's notes

My first instinct was to think about it like a sorting algorithm and count swaps, which was wrong.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

The minimum number of moves to sort an array by moving elements is equal to the length of the array minus the length of the longest non-decreasing subsequence (LNDS). This is because elements in the LNDS are already in correct relative order and can remain in place, while all other elements must be moved. So, compute the LNDS using dynamic programming or patience sorting, then subtract its length from n.

Pro tip: Clarify that 'move' means removing an element and inserting it anywhere else; if the operation were a swap, the answer would differ. Also, mention that duplicates are allowed and the LNDS handles them correctly by using non-decreasing order.

1. Understand the problem

Confirm that a move operation takes one element and inserts it at any other position, shifting other elements. The goal is to minimize the number of such moves to achieve a sorted (non-decreasing) array.

2. Identify the invariant

Elements that are already in correct relative order (i.e., form a non-decreasing subsequence) do not need to be moved. The largest such subsequence can stay, minimizing moves.

3. Relate to LNDS

The minimum number of moves equals n minus the length of the longest non-decreasing subsequence (LNDS). This is because all elements not in the LNDS must be moved, and we can always insert them in the correct positions.

4. Compute LNDS efficiently

Use dynamic programming O(n^2) for simplicity, or patience sorting with binary search O(n log n) for optimal performance. For the example [5,2,2,4,3], the LNDS length is 3 (e.g., 2,2,4 or 2,2,3), so moves = 5 - 3 = 2.

5. Verify with example

Show that moving 5 to the end and 3 before 4 (or similar) sorts the array in 2 moves. Confirm that 1 move is impossible because no single move can sort it.

Key Points to Mention

  • Definition of a move operation: remove an element and insert it anywhere else.
  • Longest non-decreasing subsequence (LNDS) as the key concept.
  • Formula: minimum moves = n - length of LNDS.
  • Handling duplicates: LNDS allows equal elements, so duplicates are fine.
  • Time complexity: O(n log n) using patience sorting, or O(n^2) with DP.
  • Example calculation: for [5,2,2,4,3], LNDS length = 3, so moves = 2.

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