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:
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):
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!
If you want updates from me on my future blog posts or on my future projects, please sign up for my email list below!
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).
LikeLiked by 1 person
Thank you for catching that mistake. I have updated it to be an actual solution now haha.
LikeLike