Palantir logo

Palantir

Denver, COSoftware

Interview Questions

Swipe Left Implementation

Asked at Palantir
technical
arrays
implementation

Implement a swipe left function that moves numbers to the left and merges equal numbers.

Function Signature:

function swipeLeft(numbers: number[]): number[]

Requirements:

  • Move all numbers to the leftmost possible position
  • Merge equal adjacent numbers when moving left
  • Fill remaining positions with zeros
  • Process merges in order from left to right

Examples:

Input: [2,2,2,2]
Output: [8,0,0,0]
Explanation: 
1. First merge: [4,4,0,0]
2. Second merge: [8,0,0,0]

Input: [0,2,2,0]
Output: [4,0,0,0]
Explanation:
1. Move left: [2,2,0,0]
2. Merge: [4,0,0,0]

Share Your Experience at Palantir