Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Sort Array of 0s and 1s

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

Your question is Sort Array of 0s and 1s. 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

Blue Yonder's Luminate planning workflows can represent a binary allocation flag as 0 or 1. Given an array containing only these values, sort it in ascending order so every 0 appears before every 1.

Modify the array in place and return the same array. Do not use Python's built-in sorting method. Aim for a single linear scan and constant extra space.

Formal Specification

  • Input: nums, a mutable array of integers containing only 0 and 1.
  • Output: The sorted nums array, containing all 0s before all 1s.
  • The relative order of equal values is irrelevant.

Constraints

  • 0 <= len(nums) <= 10^5
  • Every element of nums is either 0 or 1
  • The array must be modified in place
  • The target complexity is O(n) time and O(1) extra space

Function Signature

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