Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Align Zeroes and Merge Lists
00:00
5 left

Align Zeroes and Merge Lists

EasyPython

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):
Interviewer

Your question is Align Zeroes and Merge Lists. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.