Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Move All Occurrences of X

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

Your question is Move All Occurrences of X. 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

AppFolio may need to reorder a property-related array so records with a particular marker value are processed last. Given an integer array nums and an integer x, move every occurrence of x to the end of the array in place, while preserving the relative order of all other values and the relative order of the moved x values.

This is a stable partition, not a cyclic rotation. Return the modified array.

Formal Specification

Implement move_value_to_end(nums, x), where nums is a mutable list of integers and x is an integer. Modify nums directly and return the same list object. The output must contain exactly the same values as the input, with all values not equal to x first, followed by all values equal to x.

Constraints

  • 0 <= nums.length <= 10^5
  • -10^9 <= nums[i], x <= 10^9
  • The solution must use O(1) auxiliary space
  • The relative order of all values must be preserved

Function Signature

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