PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates array-manipulation and in-place algorithm skills, including understanding of sorted-data merging, index/pointer management, and analysis of time and space complexity within the Coding & Algorithms domain.

  • hard
  • Amazon
  • Coding & Algorithms
  • Software Engineer

Merge two sorted arrays in-place

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: hard

Interview Round: Technical Screen

Given two arrays `arr1` and `arr2`, both sorted in **ascending** order. - Modify `arr1` **in-place** to contain all elements from `arr1` and `arr2` in **ascending** order. - **Do not** allocate an additional array for the merge result. Assumptions/clarifications you may use: - `arr1` has enough extra capacity at the end to hold all elements of `arr2` (e.g., `arr1` length is `m + n`, where the first `m` entries are valid and the last `n` are empty slots), and `arr2` has length `n`. - You are given `m` and `n`. Follow-ups: 1. How would you adapt your approach if both arrays were sorted in **descending** order? 2. If the arrays were **not sorted**, what would you do (and what are the time/space trade-offs)? Define the time and space complexity of your approach.

Quick Answer: This question evaluates array-manipulation and in-place algorithm skills, including understanding of sorted-data merging, index/pointer management, and analysis of time and space complexity within the Coding & Algorithms domain.

Given arr1 with trailing capacity and arr2, return arr1 after merging both sorted arrays.

Constraints

  • The last n positions of arr1 are free capacity

Examples

Input: ([1, 2, 3, None, None, None], 3, [2, 5, 6], 3)

Expected Output: [1, 2, 2, 3, 5, 6]

Explanation: Standard merge from the back.

Input: ([0], 0, [1], 1)

Expected Output: [1]

Explanation: arr1 has no valid initial values.

Input: ([2, None], 1, [1], 1)

Expected Output: [1, 2]

Explanation: arr2 value belongs before arr1 value.

Input: ([1, 2], 2, [], 0)

Expected Output: [1, 2]

Explanation: arr2 empty.

Hints

  1. Fill from the end so unmerged arr1 values are not overwritten.
Last updated: Jun 27, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Top-p (Nucleus) Sampling in NumPy - Amazon (medium)
  • Implement Multi-Head Attention from Scratch in NumPy - Amazon (medium)
  • Detect and Break a Cycle in a Singly Linked List - Amazon (medium)
  • Caesar Cipher with Translation-Table Optimization - Amazon (medium)
  • Minimum Drone Delivery Time on a Ring of Hubs - Amazon (medium)