Negatives before non-negatives
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 8 minutes. Free to practice.
Move all negative numbers before the non-negative ones without changing the relative order inside either group. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A report wants loss values first and zero or positive values after them, but each group must keep the arrival order. Return a new list with all negative numbers first, followed by zero and positive numbers. Preserve relative order within both groups and leave the input unchanged. Write partition_signs(nums: list[int]) -> list[int]. [3, -1, 0, -5, 2] becomes [-1, -5, 3, 0, 2]. The starter file already contains the function name, harness and print lines. Keep them and fill only the function body.
Requirements
- Put every negative before every non-negative.
- Preserve order inside each group.
- Build and return a new list.
Constraints
- Zero belongs to the non-negative group.
- The return type is a list of integers.
Examples
Input: partition_signs([3, -1, 0, -5, 2]) Output: [-1, -5, 3, 0, 2] -1 stays before -5, and 3, 0, 2 keep their input order.
Topics: lakebench, python, partition, stable.
More Python interview questions · All interview problems · Learn data engineering
Negatives before non-negatives
Interview-style drill: Move all negative numbers before the non-negative ones without changing the relative order inside either group.
Implement `partition_signs(nums: list[int]) -> list[int]`. Return a new list that has all negative numbers first, then all non-negative numbers (zero counts as non-negative). Inside each group the numbers keep the order they had in the input. The input list must not change.