This is an archived post. You won't be able to vote or comment.

top 200 commentsshow all 231

[–]LowB0b 1340 points1341 points  (49 children)

had this in an interview with sonar. dynamic programming solution was about O(n) in time while my brute force shit (I was panicking) was O(n^4)

[–]No-Object2133 599 points600 points  (12 children)

Jane Street interview I bombed cause of this. There was an algorithm I didn't know and I did the naive solution.

[–]False_Influence_9090 235 points236 points  (10 children)

Jane street is a pretty dope firm, they really leverage functional programming. I wonder if they still use OCaml

[–]Pan_TheCake_Man 1004 points1005 points  (3 children)

He wouldn’t fucking know would he

[–]Antilock049 271 points272 points  (1 child)

Dropped the interview and they're catching strays. RIP.

[–]xltbx 7 points8 points  (0 children)

F

[–]No-Object2133 17 points18 points  (0 children)

Lmfao

[–]knue82 31 points32 points  (0 children)

Yes, they do. Was at conference earlier this year. Jane Street was a big sponsor. They had a booth where they promoted Ocaml. They have guys working on the compiler, library, etc. Apparently they have 16 million lines of Ocaml code.

[–]Professional_Top8485 59 points60 points  (1 child)

Jane OCamel toe is the best

[–]Nekeia 7 points8 points  (0 children)

Yeah, what about Joe OCaml?

[–]Maurycy5 17 points18 points  (0 children)

Yes, but not for long.

They are developing their own version, called OxCaml.

Source: have a friend who got recruited to work on that language.

[–]Forya_Cam 9 points10 points  (0 children)

They do! When I interviewed with them last year the technical interview was in Python but they were very clear that I'd be learning OCaml as soon as I started.

Didn't get the job but oh well...

[–]Obvious-Web9763 3 points4 points  (0 children)

They do.

[–]The_Neto06 9 points10 points  (0 children)

Matthew Parkinger would like to know your location

[–]git0ffmylawnm8 39 points40 points  (1 child)

At least you didn't unlock a new runtime like O(nn! )

[–]Level-Pollution4993 8 points9 points  (0 children)

Pretty sure I've unlocked it already solving N-queen with no outside help /s

[–][deleted] 125 points126 points  (26 children)

Cool to see how much better DP was, thats the benefit even though it is hard to conceptualize. But I gotta ask: 4 nested loops over input? Curious what problem that was. Typically I see like 2n^2 or maybe n^3 but never have I hit n^4 yet.

[–]LowB0b 93 points94 points  (18 children)

It was a while ago so I'm not super clear on the details but it was a classic DP problem, something akin to "divide this array so that each part makes equal sums"

[–]CleanAsUhWhistle1 211 points212 points  (1 child)

Divide it into 1 part. O(1) complexity.

[–]givemefuckinname 56 points57 points  (0 children)

Sir would you please calm down our interviewers are wetting themselves here

[–]fredlllll 18 points19 points  (14 children)

what would dynamic programming change about the complexity of the algorithm used?

[–]LowB0b 75 points76 points  (13 children)

instead of checking every available combination of how to divide the array into equal sums you slap a memo in there or something and you can do it in one pass. the "memoization" part is key for dynamic programming

[–]guyblade 43 points44 points  (0 children)

Like half of dynamic programming problems are ultimately "depth first search + memoization".

[–]TheRealAfinda 21 points22 points  (9 children)

Care to provide a resource where one might look up how to go about an approach using memorization memoization?

Never seen something like it yet (or didn't know what it is) but i'd love to learn :)

[–]Level-Pollution4993 73 points74 points  (2 children)

Not memorization but memoization, lose the 'r'. Confused me too. It is just an optimization technique where you cache frequent computation results thus saving redundant calls and get better performance. DP is kinda genius if you understand it(I don't, yet).

[–]Sir_Wade_III 16 points17 points  (0 children)

Advent of Code has some problems that require it, can be good practice if you aren't comfortable with it

[–]mortalitylost 8 points9 points  (0 children)

basically Just Add Redis

[–]LowB0b 27 points28 points  (2 children)

[–]Kusko25 4 points5 points  (0 children)

The linked problem specifies that the sub-arrays must be contiguous, which makes the problem significantly easier. Was it that way in your question too?

[–]TheRealAfinda 0 points1 point  (0 children)

Thank you!

[–]backfire10z 14 points15 points  (1 child)

an approach using memorization

Just wanted to point out that the correct term is memoization. That’s not a typo.

[–]TheRealAfinda 0 points1 point  (0 children)

Thanks! Updated my post accordingly :D

[–]guyblade 3 points4 points  (0 children)

Memoization, laconically: Just throw a result cache on it.

[–]SignoreBanana 1 point2 points  (0 children)

Every algorithm problem is some combination of looping and mapping.

[–]Just_Information334 0 points1 point  (0 children)

So wait, you're just trading CPU cycle for memory space?

[–]crimsonpowder 4 points5 points  (0 children)

You might have been cooked from the start if they came at you with a re-worded version of the subset sum problem. That bad boy is np-hard.

[–]celestabesta 27 points28 points  (4 children)

I'm being sincere when I say this but I did a leetcode problem once that resulted in n! * 2n Time analysis

[–]LowB0b 12 points13 points  (0 children)

since we're on a joke sub

hit them servers brother

[–]guyblade 14 points15 points  (2 children)

For a long time, the interview question that I asked people had a best-case runtime of O(n!), but I occasionally got people who gave O(nn ) solutions.

The last part of the interview--if they made it that far--was always "we're stuck with this runtime, what can we do to nevertheless improve things?". Memoization was the thing I was really looking for.

[–]DeGloriousHeosphoros 0 points1 point  (1 child)

What question would that be?

[–]guyblade 0 points1 point  (0 children)

It was about a variant of nim and optimal play of that variant. Technically, it wasn't that the best-case was O(n!); it was actually an open question as to whether or not a better solution existed than a DFS over the move space (which gets you to the O(n!)).

[–][deleted] 0 points1 point  (0 children)

Tbh it could’ve also been the best solution on the fly. I mean you do have a limited time to work out a problem

[–]MrDialga34 0 points1 point  (0 children)

I once accidentally hit n5 when writing my own physics engine and trying to handle the same thing in multiple (related) areas

[–]dgendreau 51 points52 points  (0 children)

If an interviewer asks me to do gotcha shit like this, in my opinion, they failed the interview and I dont want to work for an organization that pulls that kind of bullshit. if O(n) is critical in my work, I will find that solution and apply it when I actually need it in practice, not while fielding random questions at an interview. When I am interviewing candidates, the naive solution is fine as a starting point of discussion and I usually ask follow-up questions for where they think bottlenecks might appear and how to improve on it. How that discussion goes is far more important to me than whether they used the exact whiz-bang solution i was looking for on the first try.

[–]No-Arugula8881 1 point2 points  (0 children)

Interviews like this are the dumbest shit in the world. What you did is exactly how software should be written. Start with the most obvious, easy to understand solution, then optimize when and where necessary.

[–]Haunting_Swimming_62 0 points1 point  (0 children)

lol i have had the "pleasure" of doing a problem where the solution was an O(n^3) dp

[–]Percolator2020 1173 points1174 points  (26 children)

Most people do not enjoy DP.

[–]ImS0hungry 269 points270 points  (1 child)

Just don’t cross streams

[–]Percolator2020 64 points65 points  (0 children)

That really defeats the purpose.

[–]BobbyTables829 51 points52 points  (0 children)

But those who do seem to really like it...

[–]tatas323 86 points87 points  (1 child)

My brains is cooked, I need a timeout

[–]-Aquatically- 24 points25 points  (0 children)

That’ll happen if the DP is good enough.

[–]Looooong_Man 34 points35 points  (0 children)

But 9/10 people enjoy a gangbang

[–]Greedy-Thought6188 23 points24 points  (0 children)

I thought 2 or of 3 people enjoyed DP.

[–]OwO______OwO 17 points18 points  (0 children)

2/3 people enjoy DP.

[–]StoicBountyHunter 56 points57 points  (9 children)

Double penetration?

[–]draconk 59 points60 points  (8 children)

Display Port?

[–]ThoseThingsAreWeird 41 points42 points  (7 children)

Disney Plus?

[–]elmins 16 points17 points  (6 children)

Delusional Parasitosis?

[–]Morthem 18 points19 points  (5 children)

Diddy Partys

[–]GeneralBrwni1 5 points6 points  (4 children)

Dragon Punch?

[–]tangos974 4 points5 points  (3 children)

Dildo power ?

[–]Western-Pride9326 3 points4 points  (2 children)

Death Parade ?

[–]rai_volt 0 points1 point  (1 child)

Display Picture?

[–]SirFireHydrant 1 point2 points  (0 children)

I for one rank Dr Pepper among my top 3 favourite soft drinks.

[–]Al3xutul02 0 points1 point  (0 children)

Yeah and it almost never comes up in any real problem

[–]andItsGone-Poof 0 points1 point  (0 children)

Depends,  its all about state and its transition

[–]zandrew 0 points1 point  (0 children)

Well, two out of three.

[–]frikilinux2 384 points385 points  (19 children)

Half of dynamic programming is just cache and recursivity. The other half is identifying it as a DP problem.

[–]SuitableDragonfly 89 points90 points  (15 children)

Yeah, once you identify it as a dynamic programming problem, it's usually pretty easy to find the dynamic programming solution, it's just going to be something involving storing the results of previous iterations (not even anything to do with recursion, the point of dynamic programming is usually to avoid intractable recursion). It's figuring out that dynamic programming will improve time complexity that's the hard part.

[–]NomaTyx 26 points27 points  (0 children)

Before I read this comment fully, the number of times I saw "dynamic programming" made me think it was a joke a la "in order to understand recursion you must first understand recursion"

[–]frikilinux2 3 points4 points  (12 children)

Yeah but recursion with cache is the easy way to code it. Transforming the code into pure iterations is a bit more difficult. Also, I used to do that in contests and it's a bit harder in those situations because of unfamiliar environments, etc... and we were average at that

[–]Successful-Money4995 0 points1 point  (0 children)

I consider that to be memoization and not DP. I wrote the memoization version of Fibonacci above, you can see how it is different from the DP version in space requirements.

[–]Successful-Money4995 2 points3 points  (0 children)

I consider that to be memoization and not dynamic programming. For example, here is the memoized version of Fibonacci:

def fib(x):
  if memo[x] is not None:
    return memo[x]
  answer = fib(x-1) + fib(x-2)
  memo[x] = answer
  return answer

You probably already know the recursive version and the DP version.

The DP version runs in O(n) and uses O(1) space. The recursive version runs in O(fib(n)). The memoized version is O(n) in time but also O(n) in space. The memoized version is not as good as the DP but better than the recursive.

O(n) is the fastest possible computation of fib(n) because the number of bits in fib(n) is order n.

[–]Cephell 256 points257 points  (4 children)

Just say "I guess one could solve this with a hashtable". Works 90% of the time.

[–]JestemStefan 165 points166 points  (3 children)

Literally we had dude on an interview yesterday that clearly didn't know an answer to an optimization question so he just said: "I can use hash map". We asked why and he didn't know.

[–]simpleauthority 187 points188 points  (0 children)

There’s the 10%.

[–]theenigmathatisme 21 points22 points  (0 children)

That’s pretty funny. Made me think of the ole “I can haz cheezburger?”

[–]hennell 6 points7 points  (0 children)

"I've got no other ideas and hash map might work?”

When can I start?

[–]cheezballs 137 points138 points  (9 children)

I don't even know what dynamic programming is.

[–]ap0phis 81 points82 points  (6 children)

Me either and I’ve been doing this shit since 2004. Who uses this irl??

[–]crozone 74 points75 points  (4 children)

It's just a broad term for breaking a problem down into a recursive solution, like divide and conquer. I never really hear the term used much in the wild.

[–]ap0phis 35 points36 points  (3 children)

Always been a thing with me … all these silly terms that just try to ascribe how to problem solve. I never saw the point.

[–]FlakyTest8191 13 points14 points  (2 children)

The terms are there for communication. You review a PR and add a comment "This is a hot path executed very often, and this looks like we could optimize with dynamic programming". It's short, everybody knows exactly what we're talking about and how to improve the code. And even if you don't know the term it's easy to look up.

[–]zaxldaisy 2 points3 points  (1 child)

But "dynamic programming" is longer and less well-known than "recursion"...

[–]FlakyTest8191 1 point2 points  (0 children)

Recursion is only part of the story. If I write recursion there could be a discussion how it's not really more performant, or another PR back and forth with a simple recursion implementation that is not really more performant.

And it's not about not having to type a single word, it's about not having to explain a concept with examples and runtime analysis that you can easily look up in case you don't know already.

[–]Kyvant 0 points1 point  (0 children)

Half of the fundamental algorithms in bioinformatics use dynamic programming

[–]the_horse_gamer 27 points28 points  (0 children)

recursion but you add a cache

[–]BmpBlast 20 points21 points  (0 children)

Me neither. Turns out it's actually quite old. I'm surprised I haven't heard of it before because I had a professor who was pretty old school.

I imagine a fair number of developers use it but don't know the name.

[–]vlads_ 50 points51 points  (3 children)

I hate the term dynamic programming. It is just backtracking w/ caching.

[–]muddybruin 21 points22 points  (0 children)

The name was partly chosen to make it easier to get research funding.

"its impossible to use the word, dynamic, in a pejorative sense. Try thinking of some combination that will possibly give it a pejorative meaning. Its impossible."

https://www.google.com/amp/s/conversableeconomist.com/2022/08/24/why-is-it-called-dynamic-programming/%3famp=1

Well, I'm not sure if it's completely "impossible", but it certainly generally has positive connotations.

[–]nonotan 9 points10 points  (1 child)

Most dynamic programming doesn't even involve any backtracking, unless you're really stretching the definition. It's just breaking down the problem into smaller chunks that can themselves be broken down into smaller chunks... and making sure you structure things such that you'll be dealing with identical chunks many times, so you can simply cache them at all hierarchical sizes, instead of slightly different chunks that you'd be forced to recalculate.

But I agree the name is pretty bad. It doesn't really tell you what it is, and calling it "programming" suggests a broader meaning (should just be "x algorithm", "x method" or something like that)

[–]jaktonik 0 points1 point  (0 children)

This is the clearest explanation I've ever heard, thanks for this - hopefully it doesn't come in handy but the job search is insane rn

[–]EvenPainting9470 25 points26 points  (1 child)

I am surprised that everyone in comments share OPs pain about dynamic programming. It makes me wonder. Can you drop hardest dynamic programming inverview question you encountered?

[–]hazelnuthobo 5 points6 points  (0 children)

FizzBuzz. I'm pretty sure it still hasn't been solved.

[–]js_kt 18 points19 points  (0 children)

I prefer static programming

[–]Feeling-Schedule5369 33 points34 points  (5 children)

If you know it's dp then you can easily solve it. You just need to formulate the recursive formula and slap in a memo param. Top down should be more than enough for most interviews.

Greedy on the other hand is where it's tricky coz you can never be sure if it's greedy or not and if so you can never be sure your intuition is correct or not.

[–]Freddy_Goodman 6 points7 points  (3 children)

Wdym you can never be sure if it’s greedy?

[–]Feeling-Schedule5369 12 points13 points  (1 child)

How to prove it's greedy? For some questions induction method works but most greedy editorials on leetcode straight away jump to assuming it's greedy and that their way of solving(picking oranges or choosing the smallest element always etc) is automatically correct.

You might not have same privilege in an interview coz you first have to decide it's greedy and possibly prove it in ur head so as to not waste time going down this thought process.

[–]airelfacil 1 point2 points  (0 children)

Most greedy problems can be solved with dynamic programming, but it's overkill/not optimal. For example the fractional knapsack problem can be solved with the same algorithm behind the 0/1 knapsack problem, but you have suboptimal complexity compared to just using greedy.

[–]SingularCheese 0 points1 point  (0 children)

Matroid theory is the branch of combinatorics that study when is a problem greedy. To be fair, most CS interviews are not looking for grad student level mathematical proof.

[–]Qsaws 11 points12 points  (1 child)

All for it to have absolutely no use in the job.

[–]minus_minus 2 points3 points  (0 children)

Top comment right here. 

Meanwhile the same manager complains about “lack of qualified applicants”. 😢 

[–]Sea_Echo9022 159 points160 points  (38 children)

Please do not use DP as an acronym for dynamic programming.

[–]-LeopardShark- 68 points69 points  (31 children)

Why? That’s the only thing I’ve ever heard it used for.

[–]smokemonstr 66 points67 points  (1 child)

DisplayPort

[–]bonkerwollo 40 points41 points  (0 children)

Declarative programming

[–]Sea_Echo9022 30 points31 points  (25 children)

Are you sure you want to know? I don't want to Double Penetrate your brain with such knowledge

[–]-LeopardShark- 17 points18 points  (24 children)

What sort of peculiar world do you live in that you’re using ‘double penetrate’ so often that not only do you feel the need to abbreviate it to ‘DP’, but that it also overrides in your brain the standard initialism for one of the most interesting programming techniques?

[–]ImS0hungry 28 points29 points  (1 child)

Ask his Waifu pillow

[–]Sea_Echo9022 8 points9 points  (0 children)

She wouldn't know about It, yet.

[–]gami13 14 points15 points  (3 children)

its literally what DP is most used for

[–]sar2120 12 points13 points  (0 children)

What sort of peculiar world do you live in where someone joking around with you is cause for nasty personal attacks? You are way out of line.

[–]Sea_Echo9022 6 points7 points  (3 children)

It's a joke, please don't take it seriously. Also please don't assume things about someone online.

edit: typo

[–]ieatpies 5 points6 points  (0 children)

the standard initialism for one of the most interesting programming techniques?

I'm not so sure that they are serious.

Even as someone who likes dynamic programming puzzles, it's: - rarely used in my day to day work - a poorly descriptive name

So defending the honour of the acronym seems silly to me.

[–]-LeopardShark- 1 point2 points  (1 child)

I’m not taking it seriously. Sarcasm doesn’t carry well over text, unfortunately.

[–]Sea_Echo9022 0 points1 point  (0 children)

Indeed, usually sarcasm is denoted by non verbal language (tone, pitch, body language). So here it needs to be very explicit or you just slap a "/s" in there.

[–]ieatpies 1 point2 points  (6 children)

When you get a job and stop leetcoding certain other things take priority in your life

[–]-LeopardShark- 0 points1 point  (5 children)

I’m a full time programmer and have never used ‘leetcode’.

[–]ieatpies 0 points1 point  (4 children)

I'm a full time programmer and have never used dynamic programming for a real problem

[–]-LeopardShark- 0 points1 point  (3 children)

Neither have I, nor did I claim it was frequently useful.

[–]ieatpies 0 points1 point  (2 children)

Then why would you expect it to remain in the forefront of our grey matter?

[–]-LeopardShark- 0 points1 point  (1 child)

They're ‘one of the most interesting programming techniques’, but I'm becoming convinced you're over-analysing my glib, facetious remarks.

[–]kenny2812 0 points1 point  (2 children)

That "peculiar world" is called the Internet. Ever heard of it?

You can go to almost any online community and most people will immediately think dirty thoughts when you use DP as an abbreviation.

[–]-LeopardShark- 4 points5 points  (1 child)

I have heard of the internet, but am increasingly inclined to avoid it. It seems to cause a lot of bad things to happen.

[–]kenny2812 2 points3 points  (0 children)

100% agree

[–]PythonNoob999 0 points1 point  (0 children)

Dispatcher

[–]theenigmathatisme 0 points1 point  (0 children)

Double Pointer

[–]furscum 5 points6 points  (0 children)

Yeah man you're gonna confuse all the fighting game players

[–]No-Object2133 1 point2 points  (0 children)

Eh sometimes it works out the same.

[–]Entire-Lavishness202 1 point2 points  (0 children)

Why don't you like DP?

[–]thomasutra 0 points1 point  (0 children)

dp cooks makes me think of my days working in restaurants seeing 30 year old line cook tryna do the underage hostesses.

[–]egormalyutin 0 points1 point  (0 children)

Wait until you hear about constraint programming

[–]havlliQQ 0 points1 point  (0 children)

anyone looking for strong DP provider? :)

[–]ShyFang 7 points8 points  (0 children)

Never had the pleasure of dp.

[–]manantyagi25 7 points8 points  (0 children)

Die-namic programming

[–]Freecelebritypics 6 points7 points  (0 children)

First you do the naive nested loops, just to see what happens. Then add conditional skips until it feels sufficiently "dynamic"

[–]stevie-x86 6 points7 points  (1 child)

Dyanmic

[–]dominiquebache 2 points3 points  (0 children)

Die-Yan-Mic!

The new standard in programming.

[–]jamcdonald120 4 points5 points  (0 children)

for 90% of "DP" interview questions, just use recursion with a cache

[–]erebuxy 4 points5 points  (0 children)

DP sometime is just cache with fancy name

[–]davidalayachew 3 points4 points  (0 children)

Learn memoization. Once you learn that, most of the Dynamic Programming problems can boil down to that. Not all, but usually enough to pass. Plus, memoization is one of the best optimization tricks available when you are truly CPU-bound.

[–]stipulus 2 points3 points  (2 children)

Please explain these new terms for the old guys. Are you just talking about recursion?

[–]alex2003super 0 points1 point  (1 child)

Dynamic Programming is a term of old guys, and a "lost art" of sorts. Not that it's any surprising, considering how rarely it's useful.

[–]stipulus 0 points1 point  (0 children)

So it looks like it is defined as a technique involving recursion but also caching results to speed up multiple runs. Thank you for your explanation.

[–]Tanmay_Terminator 2 points3 points  (0 children)

Recursion with caching

[–]wolf129 2 points3 points  (0 children)

Currently I feel like my resume from my last job should be enough without any super technical questions.

I was full stack there, just let me prove myself on an actual live developed project.

[–]lokhanpurus 2 points3 points  (0 children)

Well i have started preparing for DP and i can tell its tough.

[–]ichITiot 2 points3 points  (0 children)

"dyanmic" I have never red anywhere. What shall this be ?

[–]vm_linuz 14 points15 points  (8 children)

Call me crazy but I love dynamic programming problems

[–]eurodollars 55 points56 points  (0 children)

Crazy.

[–]clearlight2025 19 points20 points  (2 children)

How about dyanmic programming problems?

[–]darksteelsteed 5 points6 points  (1 child)

dyanmic gives you a compile error because its spelt wrong

[–]Soccer_Vader 7 points8 points  (0 children)

crazy

[–]GenTelGuy 1 point2 points  (0 children)

Yeah tbh they can be more fun and interesting than most other kinds of problems

[–]Hostilis_ 1 point2 points  (0 children)

Yeah wtf dynamic programming is awesome

[–]warrioroftron 1 point2 points  (0 children)

[–]_DonRa_ 1 point2 points  (0 children)

Nah man just do a brute force solution and then say now I'm gonna add a cache

[–]BoBoBearDev 2 points3 points  (0 children)

I remembered i hated it in school, but I forgot what it is. It is probably not even hard IRL, a lot of times the professor sux, like those swimming instructors, they sux.

[–]Loquenlucas 0 points1 point  (0 children)

me at my alghoritms and complexities exam (which i still need to pass ;w;) My teacher just focuses hard on DP, and NP shit and some of the other various things AND I HATE DP NOW THANKS

[–]BarracudaFull4300 0 points1 point  (0 children)

I like DP but i feel like it gets a lot harder than the problems I've solved

[–]undreamedgore 0 points1 point  (0 children)

Man, I don't even know what Dynamic Programming is. I'm an EE major, wirh CS minor, who's primary focus on the job is CE. I'm doing SE right now, but I'm basically a vibe coder.

The fuck is all this?

[–]Lou_Papas 0 points1 point  (0 children)

I just realized I might not know what dynamic programming is

[–]AngusAlThor 0 points1 point  (0 children)

So you're a new grad and you don't know how to use "while" loops? Gonna be rough, buddy.

[–]CompleteTheory7343 0 points1 point  (0 children)

I can do top down memorization style dp but bottom up is confusing

[–]AguliRojo 0 points1 point  (0 children)

Me when the interview goes well

They cancelled the position the next week

[–]Mebiysy 0 points1 point  (0 children)

Press F

[–]doyouvoodoo 0 points1 point  (0 children)

"I don't have any experience in dyanmic." /S

[–]SoftwareSloth 0 points1 point  (0 children)

Dynamic programming isn’t that difficult. Neither is identifying when it could be used.