Skip to content
CodeKruPro

Merge two sorted lists

Coding problemEasy-MediumAsked in: Capgemini

Given two lists of integers a and b, each sorted from smallest to largest, return one list that holds all values of both, sorted from smallest to largest.

Duplicates are kept: a value that appears twice in total appears twice in the result. Either list may be empty. Interviewers expect a merge that walks both lists once, not joining them and sorting again.

Examples

Example 1
Input: a = [1,3,5], b = [2,4,6]
Output: [1,2,3,4,5,6]
Example 2
Input: a = [1,1,4], b = [1,9]
Output: [1,1,1,4,9]
Example 3
Input: a = [], b = [3]
Output: [3]

Constraints

  • a and b each hold between 0 and 100,000 integers
  • Values are 32-bit integers and each list is sorted ascending

Run checks the 3 examples. Submit also runs the hidden tests: edge cases, large inputs and a time limit.

Run your code to see the results here.

This question is part of Pro+

Pro+ opens every question in Interview Prep and the mock interview. Every article and the first 2 questions of each topic stay free. Hands-on Selenium and API tasks follow their course lesson's own lock.

See Pro+

Already have Pro+? Sign in