Question bank › Backtracking
Dsa Backtracking Medium

The Gallery Wall

You are hanging frames on a wall of fixed width W. You have a set of N frames, each with a width and a color (represented by an integer). You must pick a subset of these frames to fill the wall EXACTLY to width W. There is one constraint: no two adjacent frames can have the same color. How many different ordered sequences of frames can you form? Input: First line contains N and W. Next N lines contain 'width color' for each frame. Each frame is unique (if two frames have the same width and color, they are still distinct objects). Example: Input: 3 10 5 1 5 1 5 2 Output: 2 (Explanation: Frame 0 and 2, or Frame 1 and 2. Sequences [0,2] and [2,0] and [1,2] and [2,1] are valid, but [0,1] is invalid because both are color 1. Sum must be 10. In this specific logic, only the combinations of distinct frame objects that satisfy the color rule and sum to W are counted.)

Key concepts

backtrackingconstraint satisfactionpermutation

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.