Skip to content
LakeBench
ProblemsCommunityPricing
Sign inStart practicing

Collapse Consecutive Duplicates

Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 10 minutes. Free to practice.

Collapse runs of equal adjacent values into one. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.

Implement collapse_duplicates(nums: list) -> list keeping the first of each consecutive equal run.

Requirements

  • Empty input returns [].

Constraints

  • Only adjacent equals collapse; non-adjacent duplicates stay.

Examples

Input: collapse_duplicates([1, 1, 2, 2, 2, 3]) Output: [1, 2, 3] Adjacent equals collapse.

Topics: lakebench, python, dedupe, run-length.

More Python interview questions · All interview problems · Learn data engineering

beginner

Collapse Consecutive Duplicates

Interview-style drill: Collapse runs of equal adjacent values into one.

Implement `collapse_duplicates(nums: list) -> list` keeping the first of each consecutive equal run. Example: `[1, 1, 2, 2, 2, 3]` → `[1, 2, 3]`. Keep the harness.