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

Show parent comments

1

u/leeeeeer May 09 '15

Ha, that looks better! Didn't think about filling the string from the start.

Although turns out the 30s time I said is plain wrong, I was actually testing out Twister at the same time and forgot my CPU was trying to mine a block :p

By curiosity I ran the two functions side by side and measured the performance difference and they ran pretty much as fast, although mine was a little ~10ms faster in average. That surprised me since it seems less efficient at first with the greater number of string operations, but I think it comes from the function calls to splice being way more expensive than even a large number of basic string concatenations using +.

2

u/androbat May 09 '15

The actual cause of the slower performance is most likely duplication. The way I wrote the algorithm wasn't particularly geared for performance (I wondered why yours was so slow).

In the last recursive call where you see 'pos + 1', what happens is that a ton of combinations wind up being calculated multiple times (that's why I use a set instead of pushing to an array). If I were to modify it to avoid these, it would be much faster.

A custom eval would boost performance (since all we have is simple left to right addition/subtraction). Copying and then splicing might boost performance (an array of strings also might be faster with a custom eval since it would auto-tokenize everything).