Merge a Sorted Delta Into a Preallocated Sorted Friend List

Last updated: November 29, 2026

Quick Overview

You have a sorted array of friend IDs with m real entries followed by n empty slots, and a sorted array of n new friend IDs. Merge the new IDs in place so the first array holds all m + n IDs in sorted order.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


You have a sorted array of friend IDs with m real entries followed by n empty slots, and a sorted array of n new friend IDs. Merge the new IDs in place so the first array holds all m + n IDs in sorted order.

A classic Meta screen opener reframed around incremental friend list updates. The interviewer is checking whether you notice that filling from the back avoids shifting elements and avoids extra memory.

What the Interviewer Expects
  • Fills from the end using three pointers
  • Explains why filling from the front would overwrite unread data
  • Copies the leftover delta entries when the main list runs out first
  • Achieves O(m + n) time and O(1) extra space
  • Tests m = 0 and n = 0
Key Topics to Cover
two pointers
in-place merge
sorted arrays
array manipulation
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • What if duplicates across the two lists must be dropped?
  • How would you merge k sorted deltas into one list?
  • If the base list lives on disk, how does the approach change?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: base of length m + n where only the first m values are meaningful, and delta of length n, both sorted ascending. Modify base in place so it is fully sorted. Return nothing (return `base...

High-Level Design

The empty slots are at the end, so write the largest remaining value into the last free position. Pointer i starts at m-1 in base, j at n-1 in delta, w at m+n-1. Compare and place the larg...


Submit Your Answer
Markdown supported