Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Alternate Sign Array

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

Your question is Alternate Sign 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

A Times of India ranking pipeline stores article scores as positive and negative integers. Rearrange the scores so the result starts with a positive value and alternates between positive and negative values whenever both signs remain available.

Preserve the relative order of positive values and the relative order of negative values. If one sign has more values than the other, append the remaining values of that sign at the end. Return a new array containing every input value exactly once.

Formal Specification

Implement rearrange_by_sign(nums), where nums is a non-empty list of integers containing only positive and negative values. Return a list of the same length. The first value must be positive whenever the input contains a positive value.

Constraints

  • 1 <= len(nums) <= 10^5
  • -10^9 <= nums[i] <= 10^9
  • nums[i] != 0
  • The input contains at least one positive or negative value
  • Relative order must be preserved within each sign

Function Signature

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