Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Align Zeroes and Merge Lists

EasyPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Align Zeroes and Merge Lists. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

ICE market-data validation pipelines may receive a sorted sequence whose zero values are placeholders for missing observations. Given this sequence and a second sorted sequence, move every zero in the first sequence to its end, then merge its non-zero values with the second sequence.

The non-zero values in nums already appear in nondecreasing order when the zeros are ignored. The values in other are also sorted in nondecreasing order. Preserve the relative order of non-zero values while moving zeros. Return a new sorted list containing all non-zero values from both inputs, followed by all zeros originally present in nums.

Formal Specification

Implement rearrange_and_merge(nums, other), where both inputs are lists of non-negative integers. Modify nums in place so its non-zero values occupy the prefix and its zeros occupy the suffix. Return the merged result as a new list.

Constraints

  • 0 <= len(nums), len(other) <= 10^5
  • 0 <= nums[i], other[i] <= 10^9
  • Ignoring zeros, nums is sorted in nondecreasing order
  • other is sorted in nondecreasing order
  • Only zeros from nums are moved to the output suffix

Function Signature

def rearrange_and_merge(nums, other):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output