Skip to content

Lottery ticket exercise

celestebarnaby edited this page Jun 30, 2017 · 26 revisions

Lottery Ticket

Suppose we want to design a system that allows users to create and participate in lotteries. The rules of the lottery are as follows:

  • Someone creates a lottery with a secret "winning number" between 0 and 100.
  • Anyone can purchase a lottery ticket for a set amount of money. A lottery ticket also has a random number between 0 and 100. If the lottery ticket's number is equal the lottery's number, that is a winning ticket.
  • You can check whether a ticket is the winning ticket and redeem a winning lottery ticket for a set amount of money.
  • You can only redeem a lottery ticket from the lottery where you bought the ticket. For instance, if you buy a lottery ticket from lottery1, you cannot redeem your ticket from lottery2, even if that ticket's number matches the winning number of lottery2.
  • Once someone has redeemed a winning ticket, the lottery is effectively over: you can still buy tickets, but there is no more money to be paid out to winner, so there would be no point to this.

Part 1

Design, using pseudocode, a program to handle this lottery system. It is critical to the system that a lottery ticket can be redeemed only from the lottery where it was purchased. You can assume make the following assumptions:

  • The creator and users have the lottery have accounts of some sort from which money can be withdrawn and deposited.
  • We have a simple way of generating a random winning number for the lottery and lottery tickets.

Do not worry about writing code that resembles any particular language--we want to see the kind of code you would want to be able to write. The goal here is to see how people naturally go about solving a problem like this.