Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Minimum Swaps in Binary Array

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

Your question is Minimum Swaps in Binary Array. 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

Given a binary array containing only 0s and 1s, find the minimum number of adjacent swaps required to rearrange it so that all 0s are at one end and all 1s are at the other end (either 000...111 or 111...000 is allowed).

Asked in the OA stage. Online Assessment DSA question.

Input and Output

Implement def minimum_adjacent_swaps(nums):. nums is a list of integers containing only 0 and 1. Return an integer representing the minimum number of adjacent swaps.

Constraints

  • 1 <= len(nums) <= 1000
  • nums[i] is either 0 or 1

Function Signature

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