Programming Pearls: A solution to Exercise 1-10

The last three exercises in this column are all conceptual problems, rather than programming problems.

Before the days of low-cost overnight deliveries, a store allowed customers to order items over the telephone, which they picked up a few days later. The store’s database used the customer’s telephone number as the primary key for retrieval (customers know their phone numbers and the keys are close to unique). How would you organize the store’s database to allow for orders to be inserted and retrieved efficiently?


My initial question when I read this was, how big is this store? My feeling was that if they were still using phone number as a primary key, they were probably pretty small and thus using a computerized database to organize orders that lasted only a few days was overkill. I’d just use a bunch of boxes.

I was about to toss the exercise as being silly when I decided to read the hints, for the heck of it. There, they suggested a non-computerized solution. A-ha!

So basically what I decided on was to stack 10 boxes across a shelf, number them 0-9, and use the last digit of the phone number as the ‘address’ for an order. (I use the last digit because I expect it to be more uniformly distributed than the first, and it’s really easy to find in a hurry.) The good thing about this is that it scales well: If you’re dealing with about 100 orders a day, you should have about 10 sheets in each box. If you move up to getting about 1000 orders a day, well, you just have to stack 10 shelves on top of each other and use 100 boxes, indexed by the last two digits of the phone number. If that’s not good enough, you can buy 10 such shelves, and use the last three digits. You could scale it farther than that, but if the order volume is large enough to merit 10000+ boxes, odds are pretty good that you’re going to need a different system.

This is roughly the scheme that Bentley’s solution suggests, incidentally.

Comments (0)

› No comments yet.

Leave a Reply

Pingbacks (0)

› No pingbacks yet.