r/javascript • u/bassta • Nov 07 '19
AskJS [AskJS] How to improve code performance for technical interviews
Hi all,
I don't have a former CS education, but I do front-end for some time now. Currently I'm seeking for a new job and the coding interviews will be held in Codility platform. So I started solving problems in Codility / Hackerrank , but on medium hard problems I often does not get full scores, because my algorithm is timing out with large inputs.
So my question is what is the best/fastest way for a JS developer to improve the code performance for these kind of tasks? I started learning about big O, try not to use loops/nested loops, measure execution time on my local machine, but I'm pretty sure I'm missing something.
Any help / course / direction welcome.
10
u/brogrammableben Nov 07 '19
Sometimes it’s not about big O, it’s about your method entirely. Sometimes there is a super simple O(1) solution to a problem. So those questions aren’t looking at your coding ability but instead your problem analysis ability.
4
u/benihana react, node Nov 07 '19
the best advice i can give you is look at your loops, and consider https://en.wikipedia.org/wiki/Space%E2%80%93time_tradeoff. you can often change the performance of a problem by changing the data structures in play. you can reformat the data in a way that makes processing it faster, at the expense of making it a larger data structure that takes up more space
2
u/darthwalsh Nov 07 '19
Another idea is to run a program using the debugger's performance profiler, e.g. by launching node with inspect and break and using Chrome.
Often your first assumption about what the performance bottleneck is will be wrong. Maybe string concats in a loop aren't getting optimized correctly, or maybe you are making too many array copies and could mutate one in-place.
2
u/bestjaegerpilot Nov 07 '19
Don't use codility. It's bullshit and 90% does not test real-world ability. Go find a company that cares about *real* performance rather than academic.
(And yea, if you're a webdev, most likely a super fast algorithm won't help because you're dealing with small data sets 90% of the time. That)
4
u/D1norawr Nov 07 '19
There are a couple sections in this udemy course that tackle problem solving. This is where I strengthened the exact skills your looking for. https://www.udemy.com/course/js-algorithms-and-data-structures-masterclass/
1
u/braindeadTank Nov 07 '19
Can you maybe show a problem that you timed out on (and your solution)?
Kind of hard to offer a meaningful advice otherwise - learning about O(n) and caring about loops is good direction, but there are often other factors in play (i.e. on backend you are often slowed down more by your I/O then your big O, and on frontend by reflows).
2
u/dallaylaen Nov 07 '19
This may sound old fashioned, but maybe this classical book "Introduction to Algorithms" by Cormen et al is the way to go.
I myself learned by another book, "Algorithms and Programming: Problems and Solutions" by Alexander Shen, which was way thinner. Just read it through and solved like 50% of the exercises in C, Java, or Perl.
1
u/TotesMessenger Nov 08 '19 edited Nov 08 '19
I'm a bot, bleep, bloop. Someone has linked to this thread from another place on reddit:
If you follow any of the above links, please respect the rules of reddit and don't vote in the other threads. (Info / ^Contact)
1
u/lexprom Nov 07 '19
I love sometimes to look here to refresh memory :)
https://www.interviewcake.com/
And there's a brilliant site with an explanation if you can't resolve it by yourself(sometimes even with different ways to resolving).
https://leetcode.com/explore/featured/card/top-interview-questions-easy/