Matchsticks to Square
MediumBacktrackingbacktracking
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
matchsticks[i] is the length of the i-th matchstick. Decide whether you can arrange **all** of the matchsticks into the outline of a square, using each stick exactly once. Sticks may be placed end to end along a side but cannot be broken.
Return true if such a square exists and false otherwise.
Examples
Input: matchsticks = [1,1,2,2,2]
Output: true
Sides: [1,1], [2], [2], [2] — each of length 2.
Input: matchsticks = [3,3,3,3,4]
Output: false
The total 16 needs sides of 4, but four sticks have length 3 and can't pair up.
Constraints
- 1 <= matchsticks.length <= 15
- 1 <= matchsticks[i] <= 10^8
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.