Question bank › Stack
Dsa Stack Medium

Arithmetic Progression Melting

Given a sequence of integers, you push them onto a stack one by one. Whenever the top three integers in the stack (let's call them a, b, c from bottom to top) form an arithmetic progression (i.e., b - a == c - b), they 'melt' together and are replaced by a single integer representing their sum. This melting can trigger further melts recursively. Input is a space-separated string of integers. Output the final stack. Example: Input: 1 2 3 6 12 1. 1, 2, 3 melts into 6. Stack: [6]. 2. Push 6. Stack: [6, 6]. 3. Push 12. Stack: [6, 6, 12]. (Not an AP because 6-6=0 and 12-6=6). Output: 6 6 12

Key concepts

stackmath

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.