r/programming • • May 09 '15

"Real programmers can do these problems easily"; author posts invalid solution to #4

https://blog.svpino.com/2015/05/08/solution-to-problem-4
3.1k Upvotes

1.3k comments sorted by

View all comments

Show parent comments

31

u/Slime0 May 09 '15

Wouldn't the process of making a lookup table require computing all permutations of the operators, in which case it's better to just print the ones that work as you find them? What's the benefit of the lookup table?

38

u/jrmrjnck May 09 '15

Yeah, the point of the question was basically to calculate that table. I don't know what he's going on about.

4

u/ILikeBumblebees May 09 '15 edited May 09 '15

Yeah, the point of the question was basically to calculate that table.

Well, that's where the "brute force" comes into play, and implementing a brute force algorithm to solve this problem is itself very trivial: you just enumerate every permutation of operators, then evaluate each permutation, test to see if the returned value is 100, and store the resulting boolean value.

The point is that it's more efficient to do this once and store the results as data than to actually compute all of the results at runtime, even with a more clever recursive algorithm.

0

u/[deleted] May 09 '15

[deleted]

0

u/ILikeBumblebees May 09 '15

All you'd achieve with that solution is make the impression that you're either showing off or have no idea what "premature optimization" means.

I don't think it makes sense to regard "let's brute-force this, and then find an efficient way to store the results so we don't have to do it over again" as either premature optimization or showing off. Quite the opposite: coming up with clever tricks to do it in fewer lines of more complicated code seems to better fit the bill on both counts.

3

u/Weezy1 May 09 '15

A good example of this that I've come across in my work: computing the date that Easter falls on for a given year. FAR more efficient to just compute / google a lookup table for the next ~50 years than to try to generate it in real time.

1

u/rooktakesqueen May 09 '15

Look it up on Google or manually craft the lookup table, sure. But if you write code to precalculate it to dump it into a table, why not just use that code in runtime and drop the extra complexity of the lookup table? It's complicated logic to write, but I can't imagine it's performance-intensive.

2

u/rooktakesqueen May 09 '15

Quite the opposite: coming up with clever tricks to do it in fewer lines of more complicated code seems to better fit the bill on both counts.

That's the very definition of premature optimization. You are adding complexity where it did not exist before, to speed up an operation that you don't yet know is a performance bottleneck.

If it were as simple as adding memoize(...) around your function definition, that's fine. But you're talking about some pretty complicated lookup logic, and about writing and managing an offline job to run the actual algorithm and store the results, and managing how to load those results from somewhere into a lookup table representation in memory, ...

2

u/ILikeBumblebees May 09 '15

You are adding complexity where it did not exist before

No; I'm removing complexity. A brute-force approach is much less algorithmically complex than any other solution: its implementation is obvious, and it takes less time to write, debug, and maintain.

But you're talking about some pretty complicated lookup logic

Examining the value of a byte stored in a memory location whose address is a function of the query parameter is "complicated lookup logic"? Seriously?

→ More replies (1)

3

u/Zarkdion May 09 '15

Well, he did say "trivially optomized."

1

u/Isvara May 09 '15

I like to octomize things. Makes 'em eight times faster.

1

u/ILikeBumblebees May 09 '15

What's the benefit of the lookup table?

A few dozen CPU cycles. A sequence of eight addition, subtraction, and/or concatenation operations v.s. reading a single value from memory.

3

u/Slime0 May 09 '15

But you have to do those operations to build the lookup table.

1

u/ILikeBumblebees May 09 '15

But only once. The prepopulated lookup table just gets compiled into your executable, and the calculations are never done at runtime.

7

u/Slime0 May 09 '15 edited May 09 '15

But you only have to do it once to solve the problem in the first place!

Edit: you totally changed your post after I replied. OK, so you're using templates or something like that to offload the lookup table computation into the compile. I guess that makes the program run faster. I'm not sure it saves you real-world time.

2

u/ILikeBumblebees May 09 '15 edited May 09 '15

Well, the point of posing the problem as an interview question is for the interviewer to use your overall approach as an indicator for how you'll deal with real-world coding problems.

The most effective solution to the problem of impressing the interviewer consists of:

  • construing the problem as a real-world scenario, and not merely an academic exercise in the first place;

  • with that in mind, considering not just the your ability to produce a computational solution to the problem, but also the practicality and cost efficiency of various possible solutions, demonstrating not just competence at computer science, but also an understanding of the organization's purposes and resources; and

  • not spilling your coffee on them.

A brute-force solution that generates a hardcoded lookup table to use at runtime might not be as clever as a recursive algorithm -- that's okay, since you'll likely have already demonstrated your ability to use recursion with some of the previous problems -- but it's both faster and easier to code, debug, and maintain, freeing up valuable developer time for other tasks, and also faster at runtime for the end user. From a business efficiency standpoint, this is a win-win approach, and explaining it to the interviewer is likely to be much more impressive than merely solving the computational problem. This is way you want people thinking when they're implementing, say, ERP logic to manage a business process containing several thousand variables, rather than just adding some digits up to 100.

1

u/thfuran May 09 '15

You're saying template metaprogramming to generate a bit packed LUT is both quicker to code and more understandable than a straightforward implementation of the solution?

1

u/ILikeBumblebees May 09 '15

You're saying template metaprogramming to generate a bit packed LUT is both quicker to code and more understandable than a straightforward implementation of the solution?

Brut-forcing the initial solution and then storing the results in a lookup table is more straightforward and understandable than the author's solution. You could certainly use a simpler lookup table, e.g. a dictionary full of boolean values, and call it a day.

The bit-packed solution would be useful implementation in a very memory-constrained context: imagine that you need to run this on a Commodore VIC-20! Far-fetched? Sure -- but remember, this is an interview question that's designed to measure your problem-solving approach: the business you're applying to isn't operating with Commodore VIC-20s, but neither are they in the business of making a bunch of digits add up to 100. Their actual business problems are much larger in scale, and do involve resource constraints, and demonstrating your ability to conceptualize how you approach solving problems in a context full of resource constraints, both technological and operational, is not a bad thing.

1

u/thfuran May 09 '15

I'm not saying bit packing is a bad thing, but I don't think I've ever seen a situation clarified by more bit twiddling. And before going down that road, I think it's important to know how often something is going to be executed or whether it's time-critical. Otherwise you might just find yourself spending a week heavily optimizing the runtime performance of an asynchronous operation that only runs once a month anyways. And that's time wasted.

0

u/weed420lord May 09 '15

How is this confusing? Usually code is run more than once, but you only have to make the lookup table once. This sub is weird...

4

u/Slime0 May 09 '15

The problem is to find all combinations that add to 100. A lookup table is not useful until you need to find combinations that add to an arbitrary number. Over-engineering the problem is not a good approach in a job interview.

1

u/ILikeBumblebees May 09 '15

Over-engineering the problem is not a good approach in a job interview.

I'd consider any approach other than brute-forcing this particular problem to be over-engineering the solution. But brute-forcing this problem is simple and trivial, and doesn't doesn't demonstrate any particularly insightful approach to coding, so I'd redirect it into an opportunity to demonstrate the way I look at the broader scope in which my solution is going to be deployed and used, in this case optimizing for business efficiency rather than algorithmic cleverness.

→ More replies (14)

1

u/wewbull May 09 '15

On a modern CPU you may well lose doing the single read. Each level of cache the read needs to go through probably adds a factor of 5-10x to the number of cycles it'll take. Completely uncached, hundreds of cycles for a single read.

You can do a lot of calcs in that time. Lookup tables aren't always the win they used to be. Depends on the chance of the result being cached.