Problem Definition

Given an arbitrary number of collections of cars:

  • Find the make and model that is common to all collections
  • Identify makes and models that are in only one collection.
  • Find the most expensive car in each collection (regardless of make and model)

For example


The way the data is organized can have a significant impact on how you model the rules.

Two solutions are presented based on different data models.

Part I uses an N:N association data model and that leads to a very simple rule model.

Part II uses a “flat” record oriented data model and converts the flat data into structured data (using 1:N associations).

Part I makes use of the reverse association (from car types to the groups(s) they belong to).

When collections of related objects are modeled this way, complex logic can be expressed quite succinctly.


Technically when we refer to “car” in the example above it’s not a specific car we’re referring to but rather the class of cars that share that make and model.

But what if the data is not structured but instead comes in as “flat” records that simply contain a field indicating which group the make and model belong to? Each record now might represent a specific vehicle (especially if we add the VIN to the record). This is actually how the data is described in the original challenge statement. In this data structure the same make and model may be repeated in different records (for the same vehicle or a different vehicle) so we’ll need a slightly different approach. This is covered in Part II.