Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Array Zero Shifting Without Extra Space
00:00
5 left

Array Zero Shifting Without Extra Space

MediumPython

Problem

Shift all zeros in the array to right of the array without using another array in O(n) complexity.

Implement move_zeros(nums) for a list of integers. Modify the list in place, preserve the relative order of all nonzero values, and return the modified list. The solution must use O(n) time and O(1) extra space.

Constraints

  • 0 <= len(nums) <= 10000
  • -10^9 <= nums[i] <= 10^9
  • The input list must be modified in place
  • The relative order of nonzero values must remain unchanged

Function Signature

def move_zeros(nums):
Interviewer

Your question is Array Zero Shifting Without Extra Space. 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
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.