Hard Leetcode, Easy with Constraint Solvers

The author shows that many leetcode-style problems become straightforward when expressed as constraint models and solved with tools like MiniZinc. Examples include coin change, stock trading, 3-number zero-sum, and largest histogram rectangle, all encoded with concise constraints and objectives. While solvers may not match the runtime of bespoke optimal algorithms, they excel in ease of modeling and adaptability when requirements evolve.
Key Points
- Many "hard" leetcode-style problems are naturally optimization or satisfaction problems and are easy to model in a constraint solver.
- MiniZinc models for coin change, stock profit, 3-number sum-to-zero, and largest histogram rectangle show concise formulations using variables, constraints, and objectives.
- Solvers may be slower than hand-tuned optimal algorithms and have unpredictable runtime, reflecting a capability/tractability tradeoff.
- Their key advantage is extensibility: adding new constraints (e.g., multi-trade stock rules and holding limits) is simple compared to rewriting algorithms.
- Using solvers for interview-style problems is both practical and pedagogically valuable, offering opportunities to teach modeling and optimization techniques.
Sentiment
The community is broadly sympathetic to the article's premise that constraint solvers are powerful and underappreciated. However, there is substantial pushback on the practical applicability of this insight to coding interviews specifically, with many noting that interviewers expect algorithmic solutions regardless of whether a solver approach is more pragmatic. The dominant mood is frustration with the LeetCode interview paradigm itself, which the article's thesis amplifies rather than resolves.
In Agreement
- Constraint solvers are criminally underutilized in both CS education and real-world software engineering, and more developers should learn to use them
- Using a constraint solver demonstrates practical wisdom — getting a correct solution quickly is often more valuable than crafting an optimal bespoke algorithm
- The article highlights how LeetCode problems are essentially mathematical optimization problems disguised as coding challenges, which solvers handle naturally
- Constraint solvers make it trivial to evolve solutions when requirements change, which is far more representative of real-world engineering than one-off algorithmic tricks
- Several commenters share winning hackathons and landing jobs by leveraging constraint solvers and linear programming for rapid prototyping
Opposed
- Constraint solvers are often dramatically slower than bespoke algorithms and cannot handle large inputs (tens of thousands of variables) without significant resources
- The whole point of LeetCode interviews is to test algorithmic thinking and cleverness — using a solver sidesteps the question being asked
- Knowing how to formulate a constraint problem is valuable, but interviewers may view it as over-engineering or avoiding the actual challenge
- Most LeetCode problems are in P, and the real challenge is finding efficient polynomial-time solutions, which solvers do not teach
- In practice, most interviewers would not accept a constraint solver answer and would require the candidate to implement the expected algorithm