r/programming • • May 08 '15

Five programming problems every Software Engineer should be able to solve in less than 1 hour

https://blog.svpino.com/2015/05/07/five-programming-problems-every-software-engineer-should-be-able-to-solve-in-less-than-1-hour
2.5k Upvotes

2.1k comments sorted by

View all comments

809

u/holypig May 08 '15

Well this asshole should stop calling himself a software engineer, since his solution for #4 is WRONG!

https://blog.svpino.com/2015/05/08/solution-to-problem-4

Try running with [52,5,3]

80

u/goomyman May 08 '15

not to mention number 3 goes beyond big int in size... so your going to have to write your own addition and output methods.

soo ya.. great questions /not. Just goes to show what you think is an easy question can be quite difficult on the spot, on a whiteboard, with pressure.

0

u/[deleted] May 09 '15

[deleted]

2

u/whooshayay May 09 '15 edited May 09 '15

PHP has no tail call optimization. So, why would you do fibonnaci recursively? An iterative solution, i.e. a for loop, would be much more efficient in PHP.

def fib (terms):
    t0, t1 = 0, 1
    for i in range (terms):
        t0, t1 = t1, t0 + t1
    return t0

Yes I know this is Python. I'd probably bugger up the syntax if i did it in PHP but you should be able to infer the iterative loop from that code.

0

u/[deleted] May 09 '15

[deleted]

2

u/whooshayay May 09 '15

Doing it recursively adds a significant memory and time overhead. It's far less efficient because you have overhead of time and memory each time you make a function call. The system has to allocate the local variables for that particular instance of the function on the stack, and save the return address. So you're creating one set of additional local variables for each recursive call, and performing additional store and jump instructions.

If your language runtime has tail call recursion, it will convert a specially written recursive implementation into an iterative loop for reasons of performance, but PHP doesn't have this feature.

In your code you can also eliminate $new1 by assigining to the function name instead.

→ More replies (2)