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/everysinglelastname May 08 '15

Good answer ! Worth noting here is that within each tree there are "overlapping sub-problems". For example, in the node that has +1, -1 and 1 as its children nodes there is the tree that starts with +2 under each one of those nodes. There are n number of final results that start with +2 but those all do not need to be recalculated each time you descend the tree for each of the children nodes (+1, -1 and 1). So, an optimization would be to save the results in a global object that can be reused. (i.e. memoization).

1

u/itsSparkky May 08 '15

That's true, I suspect you may see some gains from working from both sides then summing the results in the middle (ie building the tree from 1-5 and 9-6 then traversing the results in opposite orders of sums.