Software Development and Programming Careers (Official Discussion Thread)

Joined
Nov 30, 2013
Messages
409
Reputation
140
Daps
527
Recursion is something I've been trying to figure exactly what it is. I know essentially a function calls itself, creating a sort of loop, but where and why is it used?

where it's used? think of fibonacci numbers. write a function that allows you to determine the nth fibonacci number. problems that are of a recursive nature like fibonacci lend themselves very well to a recursive algorithm. however, in most cases recursion is expensive.

as for why it's used, that's not a question i can fully answer.

algorithms and big oh is a staple of things. if you cant analyze algorithms, you cant really program lol. its quite easy too, nothing complicated as long as you can do medium-level math.
I wouldnt put it as a way of meeting deadline, its just one of those things you use all the time to make design decisions. its really a building block of things that you just kinda have to know. I dont think theres a way around it once you write code that resembles real-world projects

cool. i like your attitude. as an engineer / programmer you should never at any point say that this piece of knowlege is not necessary. you should always be hungry to know more.
 

Spatial Paradox

All Star
Supporter
Joined
May 16, 2012
Messages
2,290
Reputation
1,120
Daps
12,147
Reppin
Brooklyn
Recursion is something I've been trying to figure exactly what it is. I know essentially a function calls itself, creating a sort of loop, but where and why is it used?

A recursive function is one that calls itself within its definition. Recursion tends to be used for problems where the results can be found by solving smaller subsets of that same problem.

i.e. Calculating the fibonacci number for n. You can calculate it using a loop. But if you know the fibonacci number for n - 1 and for n - 2, you can just add those two results together

A (very naive) fibonacci function using recursion:
Code:
int fibonacci(int n)
{
    if n < 2
        return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}
 

yseJ

Empire strikes back
Joined
Apr 30, 2012
Messages
44,242
Reputation
2,516
Daps
63,574
Reppin
The Yay
recursion is hard to explain, Im sure theres some very good explanations online. its basically dividing up the algorithm into more smaller solutions and have a 'memory' where you can store intermediate results (a stack)
you can explain it mathematically, computational theory-wise or low-level lol.

theres tons of real life implications
 
Joined
Nov 30, 2013
Messages
409
Reputation
140
Daps
527
A (very naive) fibonacci function using recursion:
Code:
int fibonacci(int n)
{
    if n < 2
        return n;
    return fibonacci(n - 1) + fibonacci(n - 2);
}

a buddy of mine was asked to write the algorithm for finding the nth fibonacci in an interview. dude was able to do it, but after he was done ...

interviewer: :ehh: not bad. aaah, so what's the run-time ...

my bud: :patrice:(fukk, if i know) Let me try at a guess ... :yeshrug: O(n^2) ???

interviewer: :ufdup: (you know you done fukked up right?) Try again breh :comeon:

my bud: :patrice: O(n^3)

interviewer: :ufdup: nah breh ... O(2^n)

my bud: :merchant:


i didn't even know the actual run-time when he was telling me the story but i was :mjlol:at him on the low because, exponential run-time is pretty much the worst it could ever get when it comes to algorithms. dude completely missed the mark.

take away from story? recursion algorithms are :obama:but the runtimes are :wtf:
 

Spatial Paradox

All Star
Supporter
Joined
May 16, 2012
Messages
2,290
Reputation
1,120
Daps
12,147
Reppin
Brooklyn
a buddy of mine was asked to write the algorithm for finding the nth fibonacci in an interview. dude was able to do it, but after he was done ...

interviewer: :ehh: not bad. aaah, so what's the run-time ...

my bud: :patrice:(fukk, if i know) Let me try at a guess ... :yeshrug: O(n^2) ???

interviewer: :ufdup: (you know you done fukked up right?) Try again breh :comeon:

my bud: :patrice: O(n^3)

interviewer: :ufdup: nah breh ... O(2^n)

my bud: :merchant:

take away from story? recursion algorithms are :obama:but the runtimes are :wtf:

That exponential run time :huhldup:

But this is actually a good example of why analyzing algorithms (or at least being familiar with them) is important. In this case, one could ask themselves why is the runtime so awful? If you visualize the recursive calls to the fibonacci function, you can see you're recalculating past values over and over again

I'd bet the interviewer might've been more impressed with this (pseudocode isn't really in a specific language):
Code:
Dictionary <int, int> fibMemo = new Dictionary<int, int>();

int fibonacci(int n)
{
    if (fibMemo[n] != null)
        return fibMemo[n];
    int result = fibonacci[n - 1] + fibonacci[n - 2];
    fibMemo[n] = result;
    return result;
}

By stashing past results for a given n, you don't have to keep constantly recalculating them. The run time drops to O(n), though you might be by making a tradeoff in terms of space
 
Last edited:
Joined
Nov 30, 2013
Messages
409
Reputation
140
Daps
527
Code:
Dictionary <int, int> fibMemo = new Dictionary<int, int>();

int fibonacci(int n)
{
    if (fibMemo[n] != null)
        return fibMemo[n];
    int result = fibonacci[n - 1] + fibonacci[n - 2];
    fibMemo[n] = result;
    return result;
}

By stashing past results for a given n, you don't have to keep constantly recalculating them. The run time drops to O(n), though you might be by making a tradeoff in terms of space

Hmmm ... I like this ... very clever. However, it's really not that different from the naive implementation tbh. Supposing that we are allowed to run each algorithm once, then this new one will actually be worse than the naive implementation because now you have the overhead with dealing with the dictionary abstract data type, and also the space requirements would be atrocious. I mean imagine if someone requested the algorithm to spit out the 100,000th fibonacci ... :huhldup:


but you're right, over time this would be a better alternative. when you think about the amortized cost, each call would eventually whittle down to O(1) which is the :blessed: runtime.

overall, i think when it comes to interviews, they don't really expect you to spit out the ideal answer all the time. but they do want you to be clever enough to know that your answer is non-ideal, and exactly why it is so. if you can be able to have an intelligent discussion with the interviewer regarding the problem at hand then you should be fine. i don't have a lot of experience with hardcore interviews though so you can take my advice with a grain of your finest salt.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,298
Reputation
5,571
Daps
83,581
I've been spending all kinds of time messing around with javascript... it's fun doing these little experiments. I need to find a free web host so I can post these experiments up.
 

keepemup

Banned
Joined
Jun 9, 2012
Messages
4,743
Reputation
-982
Daps
5,349
a buddy of mine was asked to write the algorithm for finding the nth fibonacci in an interview. dude was able to do it, but after he was done ...

interviewer: :ehh: not bad. aaah, so what's the run-time ...

my bud: :patrice:(fukk, if i know) Let me try at a guess ... :yeshrug: O(n^2) ???

interviewer: :ufdup: (you know you done fukked up right?) Try again breh :comeon:

my bud: :patrice: O(n^3)

interviewer: :ufdup: nah breh ... O(2^n)

my bud: :merchant:


i didn't even know the actual run-time when he was telling me the story but i was :mjlol:at him on the low because, exponential run-time is pretty much the worst it could ever get when it comes to algorithms. dude completely missed the mark.

take away from story? recursion algorithms are :obama:but the runtimes are :wtf:
Nah breh. The most important part of the story is 'did he get the job?' which you left out.
 

keepemup

Banned
Joined
Jun 9, 2012
Messages
4,743
Reputation
-982
Daps
5,349
Recursive algorithms are good when you've got a lot of ram to waste. Don't try that sh*t in embedded. Don't you dare. But then again those types of problems/algorithms usually don't have a use in an embedded system.
 
Joined
Nov 30, 2013
Messages
409
Reputation
140
Daps
527
Nah breh. The most important part of the story is 'did he get the job?' which you left out.

hell naaw man. that's why i used the mj smiley. dude fell on his face.

let the interview story be a lesson to you nikkaz who like to go into interviews with a swagger in ya step, on some 'I'm the next Q' 'The Next Zuckerburg' steez ...

:ufdup: know ya runtimes brehs. it could be the difference between being a Starbucks Barista and being gainfully employed.
 

keepemup

Banned
Joined
Jun 9, 2012
Messages
4,743
Reputation
-982
Daps
5,349
hell naaw man. that's why i used the mj smiley. dude fell on his face.

let the interview story be a lesson to you nikkaz who like to go into interviews with a swagger in ya step, on some 'I'm the next Q' 'The Next Zuckerburg' steez ...

:ufdup: know ya runtimes brehs. it could be the difference between being a Starbucks Barista and being gainfully employed.

Lol. But ol' boy should know his algorithm run times. Big O notation is easy once you have the algorithm.
 

Spatial Paradox

All Star
Supporter
Joined
May 16, 2012
Messages
2,290
Reputation
1,120
Daps
12,147
Reppin
Brooklyn
Hmmm ... I like this ... very clever. However, it's really not that different from the naive implementation tbh. Supposing that we are allowed to run each algorithm once, then this new one will actually be worse than the naive implementation because now you have the overhead with dealing with the dictionary abstract data type, and also the space requirements would be atrocious. I mean imagine if someone requested the algorithm to spit out the 100,000th fibonacci ... :huhldup:


but you're right, over time this would be a better alternative. when you think about the amortized cost, each call would eventually whittle down to O(1) which is the :blessed: runtime.

Even for a single run, it makes a massive difference. On my machine (a laptop with a quad-core Core i7 w/2.3GHz base frequency), the naive implementation of the Fibonacci function in C# takes about 15 seconds when n = 45. For the modified version using a dictionary, I can't even tell you how long it takes because it completes almost immediately from my POV. The overhead of the dictionary has more of an influence on the space complexity, which ends up being O(n).

As an aside, I decided to look up the 100,000th Fibonacci number. It's as ridiculous large as it sounds :merchant:

overall, i think when it comes to interviews, they don't really expect you to spit out the ideal answer all the time. but they do want you to be clever enough to know that your answer is non-ideal, and exactly why it is so. if you can be able to have an intelligent discussion with the interviewer regarding the problem at hand then you should be fine. i don't have a lot of experience with hardcore interviews though so you can take my advice with a grain of your finest salt.

C/S. You should at least be able to talk about why a given solution to a problem is or isn't ideal, and why.
 

kevm3

follower of Jesus
Supporter
Joined
May 2, 2012
Messages
16,298
Reputation
5,571
Daps
83,581
I'm having a blast with programming. This is one thing I love. I just wish I started earlier, but oh well. I'm finally starting to get a handle on node.js. Once I'm really able to ingrain using node js and talking to the database, my 'web apps' can really start to take off.
 
Top