Hey guys, just got a code challenge while applying to a software engineering position at Button.
This is pretty cool! Not the most difficult problem in the world, but some definite “gotchas” if you’re not careful.
Here’s the “gist” of the challenge:
So at first, glance, here’s my thought process:
- We need to know which numbers are negative (-)
- Let’s not worry about which numbers are positive, we’ll just add everything together in the end(reduce?) after turning the (-) numbers to be negative.
- We need to split the string with the right regex pattern.(+ or whitespace perhaps?)
- oh crap, this is harder than I originally thought, haha!
Ok so this is sort of the process of software engineering. Hearing a problem, coming up with a simple ish approach, realizing it’s much more involved that previously thought, and iterating on that process until you have a solid solution.
As I write tests and figure out the best approach I will update the post. For now, I have a simple implementation that’s working for almost all of the test cases, but the most difficult part of this challenge is negative numbers and parsing those correctly to add those negative numbers with reduce.
One thought is to iterate through the string, initialize a `current_operator` variable, and add that current operator to each character in the string. That way, each operator would be added to the corresponding expression, and therefore leave all of the numbers with a + or – to their corresponding value. This would make it easy to use `reduce` to calculate the sum.
Stay tuned for an updated post, complete with the process for test coverage and a JS implementation.
Leave a Reply