r/learnprogramming • • Apr 05 '22

Name one programming/comp sci concept you never understood and if you understand it, try to explain it to them

Name a programming concept such as virtual functions, pointers, certain algorithms, etc that you always had a hard time understanding and why.

Someone else who reads yours and does understand it, try to explain it to them to help them learn

1.6k Upvotes

738 comments sorted by

View all comments

Show parent comments

5

u/MrSloppyPants Apr 05 '22 edited Apr 05 '22

A very simple way is to look at how many times you loop within the algo. If you take in an array and pick out the first element, that operation is O(1) because it doesn't matter how big the array is, you are always executing the same thing. If you take the array and loop over it once, your algo is O(n) where n is the number of elements in the array. if you loop over the array twice, your algo is O(2n). If you loop through the elements in the array and for each element you loop through the entire array again, your algo is O(n2) because you are looping through the array for each element in the array. If you are only looping through some of the array, say, as part of a binary search, then the algo is O(log n) which is faster than O(n) because you are not traversing the entire contents of the array.

This is way over-simplified but it's a good starting point

1

u/SlowbeardiusOfBeard Apr 05 '22

Thank you!! This really helped!

2

u/Marvsdd01 Apr 05 '22

This is a very helpful comment, but the case isn't so obvious always, and you could even perform badly on an interview if you use this kind of assumption always. Complexity is a pretty important concept in software development, as it can help you optmize your code, but it is also important for defending changes you are adding to a shared code base, for example. There will be not-so-obvious cases, code that have the complexity depending on more than one variable (something like O(m*n), when m and n are not related), and so on. Complexity of recursions is something I struggle myself, for example. I'm an interviewer myself, and while I would never reject someone just because she/he doesn't know about complexity analysis, I can assure you that people perform pretty well and show much more confidence when they know these things, just because it is such a common thing to appear during an interview.