Friday, 15 January 2010

java - What is the MEMORY complexity of this fibonacci sequence algorithm? -


I recently had a technical interview and I was asked to report the memory complexity of the following algorithms written on the whiteboard.

To be more specific, if I remember correctly, it was referring to "heap space":

  public int [] fib = new int [1000]; Public Intimate Fibonacci (Int Eye) {if (i == 0) returned to 0; If (i == 1) returns 1; If (fib [i]! = 0) returns fib [i]; Fib [I] = Fibonacci (I-1) + Fibonacci (I-2); Returns fib [i]; }  

Because he said "heap space", it seems like he was giving me a clue that he wanted to give me the complexity of the following line code:

  public int [] fiber = new int [1000];  

I think I remember learning in school that in Java there is new in the form of malloc , where malloc < / Code> Allocates storage to a heap, thinking that my memory works correctly, now go back to my question: What is the complexity of this memory? Did I want to say o (1000)? On)? anything else?

Thank you!

"post-text" itemprop = "text">

I think your interviewer wanted to know how well you understood the results of the written code. The usual potential memory problem with the non-tail call recursive code is that every recurring call has a stack frame, which is not complete until the call (including all its recurring subclasses). Heap frames are allocated with a pile, so the recycling can end the pile.

Without a memoimization shortcut that saves and retrieves the already calculated Fibonacci number, memory complexity will be o (n 2 ): nth Fibonacci number calculation According to the ratio of the square of the constellation, a recursive tree will be formed, it re-calculates the same number for different branches of the tree.

But the post code should not count a Fibonacci number more than once, and the total number of the stackframe will not explode in the same way, it will be placed in O (n) worst position, where the array starts After the array is populated, it will be o (1), because from that point it is equal to seeing an array.


No comments:

Post a Comment