Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Move Non-Zero Values Left

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

Your question is Move Non-Zero Values Left. 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

In a Meta-style feed processing task, you are given a list of integers and need to shift all non-zero values to the left side of the list while keeping their original relative order. All zeros should be moved to the right.

Write a function that modifies the list in place and returns it.

Formal Specification

  • Input: A list of integers nums
  • Output: The same list after all non-zero elements have been moved to the left and all zeros moved to the right
  • The relative order of non-zero elements must remain unchanged

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • Preserve the relative order of non-zero elements
  • Modify the list in place using O(1) extra space

Function Signature

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