Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Alternate Sign Array
00:00
5 left

Alternate Sign Array

EasyPython

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):
Interviewer

Your question is Alternate Sign Array. Start with the requirements in the Question tab.

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.
CodePython 3
Sign up free to run your codeLog inLn 2
Run your code to see test output here.