Here's an interesting problem that I've seen in programming interviews, as well as the most efficient way to solve it. It has an expensive input time, but an O(1) retrieval time, which is generally considered a metric of success for algorithmic complexities when you can expect more reads than writes.
Exercise – multiplication
Consider the following code (https://github.com/PacktPublishing/Hands-on-JavaScript-for-Python-Developers/tree/master/chapter-5/matrix/starter-code):
const a = [1, 3, 5, 7, 9]
const b = [2, 5, 7, 9, 14]
// compute the products of each permutation for efficient retrieval
const products = { }
// ...
const getProducts = function(a,b) {
// make an efficient means of retrieval
// ...
}
// bonus: get an arbitrary key/value pair. If nonexistent, compute it and store it.
So, what's the solution within the paradigm of using an object? Let's take a look, break it down, and then reverse-engineer our use of objects as a data...