Skip to content

Instantly share code, notes, and snippets.

@thomaschandler
Last active October 27, 2016 20:28
Show Gist options
  • Select an option

  • Save thomaschandler/b7daccbd8d64982cb8a25c7c3feaf6ac to your computer and use it in GitHub Desktop.

Select an option

Save thomaschandler/b7daccbd8d64982cb8a25c7c3feaf6ac to your computer and use it in GitHub Desktop.
Tickets

You are a developer and you are getting calls from a bar looking for help. Each night, the bar charges a cover, and in exchange guests are given a series of numbered drink tickets that can be used to exchange for drinks.

The bar staff have been complaining about the difficulty of keeping track of all the tickets during a night, so you have been asked to come up with a solution.

For the bar, you develop a "ticket validation system" that can be used with the terminals located at the bar in order to prevent guests from re-using tickets. This ticket validation window ensures the following:

  1. A ticket with the same number cannot be used twice

  2. A ticket with a value that is too small cannot be used (ex. you took tickets that your friend gave you from another other night and attempt to use them)

  3. Ticket_counter = 10. No other tickets have been received.

Bit 7 6 5 4 3 2 1 0
Ticket Number 3 4 5 6 7 8 9 10
Used? 0 0 0 0 0 0 0 1
  1. Ticket 9 presented. It is accepted, and the field is marked.
Bit 7 6 5 4 3 2 1 0
Ticket Number 3 4 5 6 7 8 9 10
Used? 0 0 0 0 0 0 1 1
  1. Ticket 9 presented again, but it is rejected.
Bit 7 6 5 4 3 2 1 0
Ticket Number 3 4 5 6 7 8 9 10
Used? 0 0 0 0 0 0 1 1
  1. Ticket 3 presented, it is accepted.
Bit 7 6 5 4 3 2 1 0
Ticket Number 3 4 5 6 7 8 9 10
Used? 1 0 0 0 0 0 1 1
  1. Ticket 7 presented, it is accepted.
Bit 7 6 5 4 3 2 1 0
Ticket Number 3 4 5 6 7 8 9 10
Used? 1 0 0 0 1 0 1 1
  1. Ticket 11 is accepted. The bit field is shifted, and ticket 11 is marked as received.
Bit 7 6 5 4 3 2 1 0
Ticket Number 4 5 6 7 8 9 10 11
Used? 0 0 0 1 0 1 1 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment