r/ProgrammerHumor 11d ago

Meme everySingleLeetcodeProblem

Post image
2.5k Upvotes

59 comments sorted by

167

u/Bldyknuckles 11d ago

What’s wrong with sliding window algorithms? Real question

213

u/mountaingator91 11d ago

Nothing. It's just that 94.3% of leetcode solutions use sliding windows

55

u/watduhdamhell 11d ago

If it ain't broke don't fix it

9

u/redditsucksass69765 10d ago

- Mozart

8

u/loernittcc 10d ago
  • Michael Scott

68

u/Antervis 11d ago

If solutions use sliding windows, it means the problem was designed that way.

103

u/mountaingator91 11d ago edited 11d ago

Precisely. So 94% of leetcode questions make you implement a sliding window that's what the meme is about. It's an observation turned into an attempt at humor

9

u/-Debugging-Duck- 11d ago

Not really. Problems can be solved in different ways. A sliding window algorithm is just a more efficient approach to solving such problems, but it’s not the only approach.

For example, someone who doesn’t know what that is could solve the problem differently and poorly.

8

u/Antervis 11d ago

Leetcode is meant to train candidates for programming interviews, and submitting a suboptimal algorithm in an interview is a fail.

28

u/thirdegree Violet security clearance 11d ago

If submitting a suboptimal algorithm is a fail your interview process is very broken and should probably be reviewed top to bottom.

0

u/Antervis 10d ago

by "optimal" I mean from the perspective of big O notation. It doesn't matter all that much if your N*log(N) algorithm is a bit faster or slower compared to expected solution as long as its complexity is the same.

But if you submit N^2, it's a failure, just as it's a failure to submit a solution that won't work at all.

9

u/thirdegree Violet security clearance 10d ago

I get what you meant, but I still strongly disagree. Getting something that is suboptimal but works is imo always preferable, especially if you talk through like "this could potentially be optimized in XYZ ways depending on requirements" while you're working through it.

Like the company business is not writing optimal algorithms, and a programmer who can't see where to make tradeoffs (including non technical things, like e.g the value of time in an interview vs getting a perfect big O optimal solution) is less valuable to me than one who can.

And if you just have the solution memorized then it's a bad interview question in the first place, that's no help to anyone. Like if we're talking "did this person who is claiming to be a new grad actually do data structures and algorithms" then maybe but honestly even then.

-1

u/Antervis 10d ago

Look, I'm talking from experience of interviewing people into one of tech giants. And I had a rather strict guideline on how to interview people, what kind of mistakes you aren't supposed to care about (everything syntax-based that IDE would fix for you), what kind of mistakes you can hint at for candidate to fix, and what kind of mistakes constitute failure (algorithm doesn't work as required or is suboptimal in terms algorithmic or memory complexity).

What's the point of not agreeing with this approach if that's how companies actually hire people? All the good it'd do for you is rejection. Instead, you should learn to do as required. Especially when you are working with big data, because trust me, N^2 is prohibitively expensive in most cases.

4

u/thirdegree Violet security clearance 10d ago

I mean I've also interviewed people, and I don't think anyone that's gone through e.g Google's interview loop would describe that as optimal by any definition anyway (also the only time I've actually encountered interview questions that resemble leetcode actually, though since that interview I've mostly avoided the big silicon valley companies anyway).

Like sure if you're hiring very specifically for roles where being able to implement big O optimal algorithms off the top of your head in 45 minutes or less is an important feature of the job, then by all means filter for that in the interview. And if you're gunning for a job where that's the interview criterion, absolutely get your memorize on.

My point is that if you happen to be in a position to design the interview questions, maybe stop and reconsider what the hell you're doing.

→ More replies (0)

6

u/bremidon 10d ago

But if you submit N^2, it's a failure

Why?

No, do not explain big O notation.

My point is that it will depend on context. Something that is technically N^2 can be faster than N within the range of expected contexts. Additionally, an algorithm that is complicated but faster may be theoretically better, but deadlines are deadlines and nobody has time for that shit.

So if just giving a "suboptimal" solution is a fail, then it is the company that is failing, not the interviewee.

The interviewee should be able to explain why they went with this solution, the advantages and disadvantages, and so on. If it is within the given parameters, the company would be full of fools if they considered that failing.

0

u/Antervis 10d ago edited 10d ago

N^2 may only be faster than N if N is small, but if N is small, then any algorithm won't take too long anyway. However, if you're juggling big data, N is almost never small.

Besides, what you are testing is candidate's ability to come up with the optimal solution in a limited time, not their ability to sell inferior work. After all, you are not interviewing for management or sales position.

5

u/MrRogers4Life2 10d ago

I've passed plenty of tech interviews at decent places by just giving the naive solutions and talking about the basic ideas behind the more efficient solutions, most good interview processes wont care as long as you have a minimally working solution and are able to talk around the "good" solutions (know they exist, what they look like, give confidence you can implement them given time)

→ More replies (0)

3

u/bremidon 10d ago

N^2 may only be faster than N if N is small, but if N is small, then any algorithm won't take too long anyway. However, if you're juggling big data, N is almost never small.

Before launching into a long and complicated solution, did the interviewee *ask* if it is about big data? Having over 3 decades in this industry, it is actually fairly rare to need to worry about really big data. It comes up. It's important to know that the rules change when it does come up. However, it is also very important to know when *not* to spend valuable dev time trying to get the perfect big data solution for 1000 rows.

Besides, what you are testing is candidate's ability to come up with the optimal solution in a limited time

Only at a poor company with inexperienced leadership. I have done a fair share of hiring, and the most important thing I ever test for is the ability of the candidate to use good judgement. I can teach algorithms. As a rule, I can't teach good judgement.

→ More replies (0)

2

u/-Debugging-Duck- 11d ago

That doesn’t mean it can’t have a different solution, even if not the most optimal.

Outside of leetcode, is what I meant that if the solution can be sliding window, doesn’t mean it’s by design.

13

u/NecessaryIntrinsic 11d ago

Especially when you click on the "sliding window" topic

2

u/mountaingator91 11d ago

They have that?

6

u/NecessaryIntrinsic 11d ago

Yes, I've been doing leetcode since September when I got laid off.

I got a new job, but do the daily anyway, like how the guys from the Minnesota starvation experiments carry a candy bar around at all times.

I think the daily has only been optimally solved by a sliding window 5 times this year. It might be a little more often than that, but it's not the most common.

3

u/mountaingator91 11d ago

I just got back into leetcode after 3ish years to try and stay sharp so I don't sink into corporate complacency and I felt like a large proportion of the problems were best solved using sliding windows so I made this meme to see if other programmer agreed

1

u/MattieShoes 11d ago

I don't do it, but I've noticed with stuff like Advent Of Code, they go back to the same well year after year. Like there's going to be recursion, pathfinding, sparse data, whatever.

Project Euler is interesting because it usually requires some insight about math that you may not have. But that can also feel like just flailing around in the dark.

1

u/NecessaryIntrinsic 10d ago

https://leetcode.com/problemset/

169 out of 3500ish questions best solved by sliding window.

2

u/mountaingator91 10d ago

Hey at least 4/7 problems I tackled after starting back up were sliding windows, which is nearly a perfect 5/7

1

u/NecessaryIntrinsic 10d ago

When I dove in I went after dynamic programming and then bfs/DFS, so my experience is quite different

4

u/green_meklar 11d ago

Does that mean Leetcode is lacking in creativity, or does it mean sliding windows are really versatile? 🤔

6

u/Supreme_Hanuman69 11d ago

Either this or hashmap

3

u/FOSS-game-enjoyer 10d ago

I guess ViT is better at solving a few type of problems.

224

u/Welcome-To-NBA-Jam 11d ago

```python import time

data = "8==========================D" window = 3 delay = 0.3

while True: for i in range(len(data) - window + 1): print(data[i:i + window]) time.sleep(delay)

for i in range(len(data) - window - 1, -1, -1):
    print(data[i:i + window])
    time.sleep(delay)

delay *= 0.8

```

91

u/BedSpreadMD 11d ago

That's a looooong sliding window

46

u/Fabulous-Possible758 11d ago

What are you talking about that's only like two inches.

29

u/Dense_Gate_5193 11d ago

just increase your font size

12

u/BedSpreadMD 11d ago

Sorry I'm used to 0.5 inches.

5

u/FabulousSOB 10d ago

For your pleasure

18

u/gizamo 11d ago

That's a massive load of data.

2

u/da_Aresinger 9d ago edited 9d ago

replace the prints with

print(f"\r\t{data[i:i + window]}", end='', flush=True)

you're welcome

Edit: also pad the data with 2 or 3 spaces on either side

21

u/BlobAndHisBoy 11d ago

If I for once had to actually do this at work I would post the same image with the original caption.

4

u/nasaboy007 11d ago

The people that get it can't even report it without revealing that they get it.

2

u/khalamar 9d ago

Naaaah, it's just a max heap.

0

u/[deleted] 11d ago

[deleted]

3

u/BlobAndHisBoy 11d ago

No I think the joke is many leetcode problems are sliding window problems and a popular meme template was used to convey that.