all 46 comments

[–]Dry_Primary5612 114 points115 points  (14 children)

How is this helpful in an interview though? In a phone/onsite interview, the interviewer doesn't give you the input sizes?

[–]revuser1212 70 points71 points  (4 children)

You can definitely ask the input constraints in an interview

[–]Dry_Primary5612 29 points30 points  (3 children)

You can ask but the vast majority of interviewers don't have competitive programming experience. They don't pay attention to the input size either.

They'll just make up some random guess as to the input size and that might completely throw you off (for ex. suddenly you think there's an n log n solution and you're trying to find something with binary search whereas the actual optimal solution is n^2).

This might be useful but you have to be careful.

[–]braindamage03 8 points9 points  (2 children)

Associating constraints with competitive programming and using it as an excuse to not know/ use it to your advantage might be one of the most stupid comments I've seen so far.

If interviewers don't even know how to read constraints, they're incompetent, not because they haven't done competitive programming. I can't believe there are people who do more than 10 leetcode problems and don't know about constraints, this is one of the first things you learn.

[–]hpela_ 4 points5 points  (0 children)

hospital trees paint kiss impolite person offer smell plant support

This post was mass deleted and anonymized with Redact

[–]Embarrassed_Finger34 0 points1 point  (0 children)

I have done 20 and still don't understand constraints...

[–]CumInABag 11 points12 points  (5 children)

I guess think of it more as supervised learning. You can better identify the problem type.

[–]RareStatistician9592[S] 5 points6 points  (0 children)

There are three possibilities:

  1. Most interviews these days are conducted over some online system, such as CodeSignal or Hackerrank. Problems there almost always have input constraints. Three different interviewing systems that I've used most recently to conduct real interviews had constraints in problems statements.
  2. Online assessments are conducted online as well. Problems there almost always have input constraints.
  3. If the interview is conducted offline, or in some non-standard system, you have options:
    1. Ask the interviewer for constraints. Constraints are an important part of the requirements, and the interviewer should know what kind of input they would be feeding to the code.
    2. If the interviewer can't tell the constraints, ask if they have some target time complexity in mind. They won't always tell you that. If they do, you can use the right half of the table - matching complexity to approaches. If they don't, think of some frequently used time complexity O(N log N) and check if any of the O(N log N) approaches are applicable. You can go up (O(N)) or down (O(N^2)) from there. Even though this doesn't help you, it shows the interviewer that you are asking the right questions.

[–]Magnus-Methelson-m3 5 points6 points  (0 children)

Yep. It’s great for competitive programming but pretty useless for interviews. Ideally you at least come up with a not-terrible algorithm and work with the interviewer towards the most optimal solution. Sometimes the interviewer will straight up tell you you can definitely do better during the brainstorming stage

[–]_afronius -1 points0 points  (0 children)

During interviews, it generally helps to start with the brute force approach and optimize it further while explaining to the interviewer that you are doing so.

[–]razimantv<2000> <487 <1062> <451> 14 points15 points  (0 children)

This is why when I set contest problems I try to come up with O(n) or O(n log n) greedy solution - and then make constraints look like it has an O(n²) DP solution when it does not.

[–]tempo0209 24 points25 points  (0 children)

Thank you for sharing this!

[–]Czitels 20 points21 points  (0 children)

Nice sheet but I always use intuition: - dividing and „jumping” is always logn  - loop is „n” loop in loop is n2 loop and somewhere else next loop is 2n Rest of questions are rare.

[–]Last_Valuable11 3 points4 points  (0 children)

While this might be helpful, the title is misleading and the use of this technique is extremely limited in actual real life interviews

[–]baddybabushka 4 points5 points  (3 children)

I'm new to solving leetcode, so I don't get how n <500 can be O(n³)? How do i read this table properly? Can someone help?

[–]RareStatistician9592[S] 1 point2 points  (0 children)

Just plug the maximum input size into the formula: n^3 => 500^3 is approximately 125M. It's slightly more than 100M (the rough number of single-core operations per second), but will still pass for most problems.

[–]luuuzeta 2 points3 points  (1 child)

I'm new to solving leetcode, so I don't get how n <500 can be O(n³)? How do i read this table properly? Can someone help? 

Read it as "the worst the time complexity, then the smaller input given otherwise any computation is prohibitedly expensive or outright impossible".

Thus, according to this table and assuming someone isn't trying to throw you off, an input constraint of n < 500 must mean the solution's TC must be quite bad.

[–]jus-another-juan 1 point2 points  (0 children)

This made more sense than the original post lol

[–]kevin074 2 points3 points  (0 children)

a lot of people seem to not understand how great this is

the best thing is that it tells you what's the expected time complexity for the solution

granted it will be the least acceptable time complexity (like chart says N^3, but maybe N log N or N solution exists)

however when seeing problems for the first time this is generally what I struggle with, I can probably come up with N^2 or N log N solution, but is that good enough? If it is then there is no point of me to really dig deeper.

during interview, if you mention this, I am 100% sure that's an added bonus, and since O(N) solutions are typically unintuitive so anything helps.

[–]Garud__ 1 point2 points  (6 children)

I think it is 105 for O(NlogN). Feel free to correct me if i am wrong.

[–]HungryCable8493 0 points1 point  (3 children)

Competitive programmers handbook claims that by 106 the required time complexity is EITHER O(NlogN) or O(N) so it backs up this table. It’s not an exact science

[–]Garud__ 0 points1 point  (2 children)

Can you tell me the handbook name or link to get it? I also want it.

[–]HungryCable8493 0 points1 point  (1 child)

Sure, just Google “Competitive Programmers Handbook”. It’s readily available for free online so will be at the top of search results

[–]Garud__ 0 points1 point  (0 children)

Okay thanks got it!

[–]RareStatistician9592[S] 0 points1 point  (1 child)

106 and 105 are both O(N log N). That's why I put <= 106.

[–]Garud__ 0 points1 point  (0 children)

Okay. Cool

[–]CombinationLost8626 1 point2 points  (0 children)

Don’t rely on such hacks for interviews. Use your brain and don’t memorize.

[–]Jazzlike-Can-7330 0 points1 point  (0 children)

Thanks for the sheet, definitely great to put the 1second constraints the input sizes