News for hackers. Stuff that matters.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by Analemma_ on Friday September 12, 2025 @11:29AM (#45223210)
Yes and no: I've asked questions like this in interviews, and I'd count it as a plus if the candidate reached for a constraint solver. They're criminally underused in real-world software engineering and this would show the candidate probably knows how to get the right answer faster instead of wasting a bunch of time.

Now, if they did answer with a constraint solver, I'd probably ask some followup whiteboard questions to make sure they do actually know how to code. But just giving a constraint solver as an answer definitely wouldn't be bad.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by qnleigh on Friday September 12, 2025 @12:09PM (#45223692)
Yes, especially if the interviewee said something like 'this may not be asymptomatically optimal, but if it's not a known bottleneck, then I might start with constraint solver to get something working quickly and then profile later.' Especially if it's a case where even the brute-force solution is tricky.

Otherwise penalizing interviewees for suggesting quick-and-dirty solutions reinforces bad habits. "Premature optimization is the root of all evil," after all.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by bluGill on Friday September 12, 2025 @12:57PM (#45224157)
Using a bad algorithm when a good algorithm that is known to exist is premature pessimization and should be avoided.

There is some debate about what premature optimization is, but I consider it about micro optimizations that often are doing things a modern compiler will do for you better than you can. All too often such attempts result in unreadable code that is slower because the optimizer would have done something different but now it cannot. Premature optimization is done without a profiler - if you have a profile of your code and can show a change really makes a difference then it isn't premature.

On the other hand job interviews imply time pressure. If someone isn't 100% sure how to implement the optimization algorithm without looking it up brute force is faster and should be chosen then. In the real world if I'm asked to do something I can spend days researching algorithms at times (though the vast majority of the time what I need is already in my language's standard library)

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by chipsrafferty on Friday September 12, 2025 @04:54PM (#45226647)
IBO premature optimization is normally one of two things:

1. Any optimization in a typical web development file where the process is not expected to be particularly complex. Usually a good developer will not write something very inefficient and usually bottlenecks come from other areas

2. Doing stuff like replacing a forEach with a for loop to be 0.5% faster

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by LPisGood on Friday September 12, 2025 @06:25PM (#45227448)
Constraint solvers (or MILP solvers) while not asymptotically optimal are often as fast or faster than other methods.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by qnleigh on Saturday September 13, 2025 @03:30AM (#45230084)
> when a good algorithm that is known to exist

Sure, if a good algorithm exists and is simple to implement, then go for it. But if it is non-trivial, then you have to make a judgement call whether it is worth the trouble to solve in a more optimal way. You acknowledge yourself that that this can take days.

Personally I really have to be disciplined about choosing what to optimize vs what to code up quick-and-dirty. There's always a temptation to write clean, custom solutions because that's more interesting, but it's just not a good use of time for non-performance critical code.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by YetAnotherNick on Friday September 12, 2025 @11:32AM (#45223241)
General constraint solver would be terribly inefficient for problems like these. It's a linear problem and constraint solver just can't handle O(10^6) variables without some beefy machine.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by nextos on Friday September 12, 2025 @12:06PM (#45223661)
FWIW, the OP's problem is not linear. It's an integer programming problem given that coins are discrete.

A trick if you can't do a custom algorithm and using a library is not allowed during interview could be to be ready to roll your own DPLL-based solver (can be done in 30 LOC).

Less elegant, but it's a one-size-fits-all solution.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by kragen on Friday September 12, 2025 @05:20PM (#45226900)
You can implement DPLL in 30 lines of code? Not for SMT, I assume.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by nextos on Friday September 12, 2025 @10:09PM (#45228747)
You'd need a fancy encoding for SAT to use a small DPLL implementation.

Otherwise, customize DPLL for this particular problem.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by NoahZuniga on Friday September 12, 2025 @11:40AM (#45223364)
O(10^6) = O(1)
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by dekhn on Friday September 12, 2025 @11:43AM (#45223403)
no, the "O" here is "on the order of", not Big O notation.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by harperlee on Friday September 12, 2025 @12:05PM (#45223652)
I believe NoahZuniga is perfectly aware of the intent and denouncing an abuse of (unneeded) notation.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by anonymars on Friday September 12, 2025 @01:12PM (#45224312)
What is "Big O" if not literally "order of"?
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by dekhn on Friday September 12, 2025 @01:57PM (#45224746)
In physics "order of" means "approximately" using something like a taylor series, which typically start with a constant, then move to higher polynomial terms which add smaller and smaller corrections. Similar, but different, I think...
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by NoahZuniga on Friday September 12, 2025 @01:18PM (#45224374)
The O stands for "Ordnung", the German word for order. So it does literally mean that, except mathematicians think that the order of f(x)=1 is the same as the order of f(x)=10^6, because "clearly" f(x)=x gets way bigger than any constant function.
Re:Many Hard LeetCode Problems Are Easy Constraint Problems by OutOfHere on Friday September 12, 2025 @11:40AM (#45223352)
Okay, but who says you need to use a simple constraint solver? There are various sophisticated constraint solvers that know how to optimize.

At this point, job interviews are so far removed from actual relevance. Experience and aptitude still matter a lot, but too much experience at one employer can ground people in rigid and limiting ways of thinking and solving problems.

Re:Many Hard LeetCode Problems Are Easy Constraint Problems by PartiallyTyped on Friday September 12, 2025 @11:42AM (#45223390)
It’d be a positive in my book if they used a constraint solver.