Move the Ones Before the Zeros
Updated
Problem statement
Given an array nums that contains only 0s and 1s, rearrange it so that every 1 comes before every 0, and return the result.
You may change nums itself and return it, or return a new array.
Function
onesBeforeZeros(nums: int[]) → int[]Examples
Example 1
nums = [0,1,0,1]return = [1,1,0,0]There are two 1s and two 0s, so the result is the two 1s followed by the two 0s.
Example 2
nums = [0,0,0]return = [0,0,0]There are no 1s, so the array stays all 0s.
Example 3
nums = [1,1,0,1,0,0,1,0]return = [1,1,1,1,0,0,0,0]Four 1s and four 0s: [1,1,1,1,0,0,0,0].
Constraints
1 <= nums.length <= 5000- Each
nums[i]is0or1
More Adobe Phone Screen questions
- Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
- All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
- Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
- Binary Tree Level-Order Traversal by LevelsPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
- Count Occurrences in a Sorted ArrayPHONE SCREEN · ONSITE INTERVIEW · Seen Jul 2026
- Count Pairs with Absolute Difference KPHONE SCREEN · Seen Jul 2026
- Count Subsets with Sum KPHONE SCREEN · Seen Jul 2026
- Numbers with Exactly Three DivisorsPHONE SCREEN · Seen Jul 2026
- Sum of Even Nodes on a Path in a Numbered Binary TreePHONE SCREEN · Seen Jul 2026
- Largest Value in Every Sliding WindowPHONE SCREEN · Seen Jul 2026
Similar questions at other companies
- Compact Conversation History by Token BudgetSierra · PHONE SCREENSierra interview questions
- Remove Duplicates from Sorted ArrayNykaa · PHONE SCREENNykaa interview questions
- Minimum Index Distance Between Person and CakeSnowflake · OA · PHONE SCREENSnowflake interview questions
- Squares of a Sorted ArrayInstacart · PHONE SCREENInstacart interview questions
- Closest Vehicle and Charger in a LineSpotify · PHONE SCREEN
- Correct an In-Place Array ReversalDuolingo · PHONE SCREENDuolingo interview questions
- Remove Duplicates From Sorted Array In PlaceKotak Mahindra Bank · PHONE SCREEN · ONSITE INTERVIEWKotak Mahindra Bank interview questions
- Merge Two Sorted ArraysCapgemini · PHONE SCREENCapgemini interview questions
- Maximize Distance to the Closest Occupied SeatAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Remove Duplicates From Sorted Array In PlaceGet My Parking · PHONE SCREEN