As programmers, we like to solve problems. In school, we thoroughly enjoy working through solutions for our homework problems. When interviewing for a developer job, we have to solve some complex programming problems (on the spot). As software developers, we still work through complex problems, but suddenly our solutions have more weight because they are solving real business problems. I enjoy solving problems that come from real-world business context; I find that my motivation to solve these problems is greater. Today at work, I came across a problem and worked through a solution that reminded me of a challenging problem one might see at school or in an interview.

Here is the problem:

You are processing potentially thousands of units of inventory and you are writing code to take inventory data from a supplier and syncing it with a distributor. The inventory is comprised of rental properties which have restrictions on specific days that are ‘closed to arrival’ and days that are ‘closed to departure’. Unfortunately, the format that the supplier stores this information is very different from the format that the distributor’s API expects. It is your job to transform this data to be in the proper format for the distributor’s API to handle.

Assume the supplier gives you data for a single unit, and that unit data is comprised of an array of inventory data. Here is the format of the a single inventory data item:

{
     startDate: 10-03-2015
     endDate: 11-19-2015
     allowedArrivalDays: {
          “M”: true,
          “Tu”: true,
          “W”: false,
          “Th”: false,
          “F”: true,
          “Sa”: false,
          “Su”: true,
     },
     allowedDepartureDays: {
          “M”: true,
          “Tu”: true,
          “W”: false,
          “Th”: false,
          “F”: true,
          “Sa”: true,
          “Su”: true,
     }
}

The distributor has a completely different scheme of storing this information. They are expecting an array of date ranges. But those date ranges must also have two fields: closedToArrival and closedToDeparture. The mapping is fairly simple, if allowedArrivalDays for a day of the week is ‘false’, then closedToArrival will be ‘true’ for that date. The main caveat is that each date range must contain only one consistent value for both closedToArrival and closedToDeparture. Here is an example of how the first few date ranges should look (using the example input data above):

[{
     startDate: 10-03-2015,
     endDate: 10-03-2015,
     closedToArrival: true,
     closedToDeparture: false
},
{
     startDate: 10-05-2015,
     endDate: 10-07-2015,
     closedToArrival: false,
     closedToDeparture: false
},
{
     startDate: 10-08-2015,
     endDate: 10-09-2015,
     closedToArrival: true,
     closedToDeparture: true
},
{
     startDate: 10-10-2015,
     endDate: 10-10-2015,
     closedToArrival: false,
     closedToDeparture: false
},
{
     startDate: 10-11-2015,
     endDate: 10-11-2015,
     closedToArrival: true,
     closedToDeparture: false
},
]

Your solution should take in the array of inventory data from the supplier and output and array of date ranges in the expected format for the distributor. This was a fun problem for me and there are a lot of different ways to approach it. If you are wanting to program and test your solution using Javascript, I would highly recommend using Moment.js to handle the dates. If there is enough interest, I will post some analysis and my actual solution in a later post. If you have any questions about the problem, please ask them in the comments below. Have fun!

Advertisements

2 thoughts on “Real-world programming interview question #1

  1. Your example “solution” mapping seems to assume that 10-3-2015 begins on a Monday (it doesn’t) and doesn’t follow your own rule for mapping for allowedArrival/Departure -> closedToArrival/Departure (your example maps true -> true and false -> false, the opposite of what you said and the names imply).

    Liked by 1 person

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s