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

5

u/[deleted] May 08 '15

Evaling every string will lead to a lot of operations duplicated.

Consider every order that starts with 1+2+3, you could create a tree that stores this order and its result of 6, then do the next operation with 4.

1+2+3+4, 1+2+3-4, & 1+2+34 is 8 operations

(10)+4, (10)-4, and (3)+34. Is 3 operations and 3 reads

This quickly scales as you build longer strings.

2

u/carnetarian May 08 '15

You're right, that's a big improvement. Have an upvote :)