Solving The Algorithm Question In Flight Entertainment

Intro:

Bryam Vicente
4 min readMar 29, 2021

Usually I’ll use Leetcode to practice problem solving, but I came across this Interview Cake problem that really caught my attention. Note that this problem will be solved in JavaScript!

The question is asking us to create a function that returns a boolean value whenever there are two movieLengths that add up to the flightLength. There are many ways to solve this problem, and many complexities to choose from, but I figured out a way to solve this problem using O(n) run time.

Using hashes tables or arrays would be a great way to solve this problem, but I’m going to use Sets which are similar to hash maps, to show another way of solving this problem. I created a variable movieTimeSet that represents the Set that I’m creating. I won’t return movieTimeSet since the question is only asking to return a boolean value. This is what the code looks like at the moment:

The next step is to iterate over the movieLengths array to check each element that’s inside the array. For this task, I’ll be using a For statement!

After creating the logic for the for loop, we need to think about the next steps that need to take place in order for the function to work. The question is asking us to return true if the array has two numbers that add up to the flightLength. We’re given the flightLength as an argument and now that we’re looping over the array, we also have the currentNumber which is movieLengths[i]. This means that we can subtract the total (flightLength) minus the current number (movieLengths[i]) and that’ll give us the missing number. I created a variable for that so that the code stays organized.

And here’s what secondMovieTime looks like so far:

Now that we got the second movie time, we need to create a conditional that checks if secondMovieTime exists or doesn’t exist in the Set. Sets having very useful built in methods that can be very helpful! Check out the resources section below for more info on those built-in methods. Here’s what I have so far:

Let’s go over lines 15 to 19 one more time. On lines 15–16 the conditional states, if the Set that we created (movieTimeSet) doesn’t have the second movie time (secondMovieTime) then add the currentNumber inside the set. If the set does have secondMovieTime then return true. After this conditional, the loop will end.

The last step is returning false after the for loop. This needs to be done because we’re stating that after checking all of the elements in the array, if there aren’t two numbers that add up to the flightLength then return false. Here’s the full solution to the problem and the expected output:

Solution
Outputs that belong to the console.logs from lines 24–26

It’s always best practice to use different test cases to ensure that the solution is working properly. Please feel free to reach out or leave a comment on this blog if there are better solutions for this problem! For more info on for statements and Sets, make sure to check out the resources section below!

Resources:

--

--