thecodewolf's blog

By thecodewolf, 3 years ago, In English

I've realised that I have a really bad habit of solving the sample/example instead of the problem itself. I come up with naive brute force and often times incorrect solution (I do this on leetcode very often) and then I submit and it fails on some test, so I fix it for that test and it fails for the next one then maybe I fix or I just give up and feel bad later that I'm not able to solve leetcode medium problems. Have you faced anything similar? Do you have any tips or suggestions for me? I understand practicing and trying to just keep at it at the problem helps, but any other practical approaches or mind tricks you might have used or know of, please share... Thanks a lot in advance!

  • Vote: I like it
  • -21
  • Vote: I do not like it

| Write comment?
»
3 years ago, # |
  Vote: I like it +14 Vote: I do not like it

One bad thing about LeetCode is that they show you tests on which your program fails, which is bad for developing debugging skills (and also prevents you from coming up with the correct solution on the first try).

My approach on Codeforces is quite different, and I don't see samples unless the problem is very easy. I think such an approach forces you to come up with a solution and then prove it as well, which is also good for interviews (I'm assuming you're doing LeetCode for interview preparation). You can test some conjectures with the samples, or make test cases of your own (for instance, edge cases) to check if your conjecture is true, so that you can try proving it (this helps if you're not good at proving your claims), and this is often a crucial step in solving observation-based problems. Moreover, once you get better at problem solving, you tend to have quite a few ideas to try for each problem before coming down to a completely ad hoc approach, and since most programming problems you will ever encounter will be solvable with a handful of tricks, it would increase your probability of solving problems as well.

There is really no trick that actually works to force any good habit, other than assuring yourself of getting better at problem solving.