Alternate Positive and Negative Values
Updated
Problem statement
Given an integer array nums containing only non-zero values, return a rearrangement whose signs alternate at every adjacent position.
The counts of positive and negative values differ by at most one. The more frequent sign must appear first; when the counts are equal, a positive value must appear first. Preserve the relative order of the positive values and preserve the relative order of the negative values.
Function
alternateSigns(nums: int[]) → int[]Examples
Example 1
nums = [3,-2,1,-5]return = [3,-2,1,-5]The counts are equal, so positive starts. Both sign groups already appear in their stable alternating order.
Example 2
nums = [-4,1,2,-5]return = [1,-4,2,-5]Positive starts because the counts are equal. Positives remain [1,2] and negatives remain [-4,-5].
Example 3
nums = [-1,4,-2,5,-3]return = [-1,4,-2,5,-3]Negative values are more frequent, so a negative starts and ends the stable alternation.
Constraints
1 <= nums.length <= 200000.-10^9 <= nums[i] <= 10^9.nums[i] != 0.- The positive and negative counts differ by at most one.
More Tesla interview questions
- Enumerate Character Paths to a MarkerONSITE INTERVIEW · Seen Aug 2026
- Shared Counter Snapshot SumONSITE INTERVIEW · Seen Aug 2026
- Flatten a Branched List into a Doubly Linked ListPHONE SCREEN · Seen Sep 2026
- Reduce-Based Map and Filter PipelinePHONE SCREEN · Seen Sep 2026
- Resilient Fake API ClientPHONE SCREEN · Seen Sep 2026
- Merge JSON Array FieldsPHONE SCREEN · Seen Aug 2026
- Count Zero-Sum FragmentsOA · Seen Jun 2026
- Maximum Insertions Without Three Consecutive AOA · Seen Jun 2026
- KV Store with RollbackPHONE SCREEN · Seen Apr 2026
- Maximum Number of Workers with Equal Left and Right ShoesOA · Seen Mar 2025
Similar questions at other companies
- Candy Crush Grid Matching and GravityRoblox · PHONE SCREEN · ONSITE INTERVIEWRoblox interview questions
- Sum Two Piecewise-Constant Time SeriesGoogle · ONSITE INTERVIEWGoogle interview questions
- Catch Fish with Reusable BaitsSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
- Collect Branches for a Bird NestVisa · OAVisa interview questions
- Score Difference When Even Picks Reverse the ListMicrosoft · OAMicrosoft interview questions
- Merge a 2048 RowPalantir · PHONE SCREENPalantir interview questions
- Add Signed Character ArraysByteDance · PHONE SCREENByteDance interview questions
- Nested List Iterator with RemoveAirbnb · PHONE SCREENAirbnb interview questions
- Match OutcomesAdobe · OAAdobe interview questions
- Rotate the Box and Drop the StonesZipRecruiter · OAZipRecruiter interview questions
Tesla interview experiences
- Tesla Experienced Infrastructure SWE Interviews: 1 Experience
- Tesla Older SWE Intern Reports: 14 Experiences (Part 4 of 4)
- Tesla Embedded and SRE Intern Loops: 15 Experiences (Part 3 of 4)
- Tesla SWE and Data Intern Rounds: 10 Experiences (Part 2 of 4)
- Tesla Intern Python Interview Prep Guide
- 2017–2026 Tesla Intern OA / Interviews: 83 Experiences (Part 2 :)