filmov
tv
Conquer the JavaScript Interview: Array Sum Finder [Intermediate Skill Level]

Показать описание
Link to this Playlist:
This is a part of my Algorithms and Data Structures playlist series. We cover a lot of common interview questions asked during whiteboards for entry level developers. Learning to master these takes time, practice, and pattern recognition. So I'll be helping you equip a toolbelt and filling it with as many tools as I can to help prepare you crush those interviews! Remember: "Luck is where practice meets opportunity."
Nested Loops O(n^2)
This function uses a nested loop to compare each pair of numbers in the array, checking if their sum is equal to the sum argument passed to the function. If a pair is found that adds up to the sum, the function returns true. If no matching pairs are found, the function returns false.
Hash Table O(n)
This function loops through each number in the array and checks if the difference between the sum argument and the current number has been seen before. If it has, we know that adding that number to the current number will give us the sum we’re looking for, so we return true. If we don’t find a matching pair, we add the current number to our seen object so we can check for it later.
This solution has a time complexity of O(n) because we only loop through the array once. It’s more efficient than the previous solution because we don’t have to check every possible pair of numbers in the array. We can keep track of the numbers we’ve seen so far in a hash table (object), which allows us to quickly check if we’ve seen the difference between the sum argument and the current number before.
Pointer O(n log n)
This function sorts the array in ascending order and initializes two pointers, left and right, to the beginning and end of the array, respectively. It then loops through the array and checks if the sum of the numbers at the current left and right pointers is equal to the sum argument. If it is, we’ve found a matching pair and can return true. If the current sum is less than the sum argument, we increment the left pointer to move to a larger number. If the current sum is greater than the sum argument, we decrement the right pointer to move to a smaller number. We keep doing this until we find a matching pair of numbers or the pointers cross each other.
This solution has a time complexity of O(n log n) because we have to sort the array first. However, it’s more efficient than the previous solution that used a nested loop because we only have to loop through the array once. We can use the sorted array and two pointers to check if any two numbers add up to the sum argument.
Don't forget to like this video and subscribe to our channel – we're publishing more videos and walkthroughs every week. Comment below and let us know what you'd like to see next!
#algorithms #javascript #interview #interviewtips #array #arrays
This is a part of my Algorithms and Data Structures playlist series. We cover a lot of common interview questions asked during whiteboards for entry level developers. Learning to master these takes time, practice, and pattern recognition. So I'll be helping you equip a toolbelt and filling it with as many tools as I can to help prepare you crush those interviews! Remember: "Luck is where practice meets opportunity."
Nested Loops O(n^2)
This function uses a nested loop to compare each pair of numbers in the array, checking if their sum is equal to the sum argument passed to the function. If a pair is found that adds up to the sum, the function returns true. If no matching pairs are found, the function returns false.
Hash Table O(n)
This function loops through each number in the array and checks if the difference between the sum argument and the current number has been seen before. If it has, we know that adding that number to the current number will give us the sum we’re looking for, so we return true. If we don’t find a matching pair, we add the current number to our seen object so we can check for it later.
This solution has a time complexity of O(n) because we only loop through the array once. It’s more efficient than the previous solution because we don’t have to check every possible pair of numbers in the array. We can keep track of the numbers we’ve seen so far in a hash table (object), which allows us to quickly check if we’ve seen the difference between the sum argument and the current number before.
Pointer O(n log n)
This function sorts the array in ascending order and initializes two pointers, left and right, to the beginning and end of the array, respectively. It then loops through the array and checks if the sum of the numbers at the current left and right pointers is equal to the sum argument. If it is, we’ve found a matching pair and can return true. If the current sum is less than the sum argument, we increment the left pointer to move to a larger number. If the current sum is greater than the sum argument, we decrement the right pointer to move to a smaller number. We keep doing this until we find a matching pair of numbers or the pointers cross each other.
This solution has a time complexity of O(n log n) because we have to sort the array first. However, it’s more efficient than the previous solution that used a nested loop because we only have to loop through the array once. We can use the sorted array and two pointers to check if any two numbers add up to the sum argument.
Don't forget to like this video and subscribe to our channel – we're publishing more videos and walkthroughs every week. Comment below and let us know what you'd like to see next!
#algorithms #javascript #interview #interviewtips #array #arrays