Programming Job Interview Challenge #8 Answer
Posted: 6/16/2008 5:48:03 PM
I've been trying to keep up with the Job Interview line of posts over at dev102.com, but unfortunately I've been running out of time to do all of them. Anyway, this weeks question can be found
here. Stop reading if you don't want the answer.
This problem can be solved using a Finite State Machine. The only thing you're going to store is the current state of the machine, once you reach the end of the machine, you'll alert. On initialization of the piping component, you'll build a finite state machine for the given alert sequence. Every time you are given a message you check what you should do with the state machine based on where you are currently at.
I was going to write out the code to do this, but it becomes a little tricky when you have repeating data in your alert sequence because if you get a message you're not expecting you don't necessarily want to reset the state machine to its initial state. For example, if the alert sequence was "A, A, A, A, B", and your input is "A, A, A, A, A, B", you don't want to reset the machine to the first state when you get that 5th A, you want it to stay in its current state.
Tags: Stuff