0

I'm not good at determining time complexities and memory complexities and would appreciate it if someone could help me out.

I have an algorithm that returns data from cache or fetch data if it's not in cache, I am not sure what its time and memory complexities would be.

What I am trying to figure out ?

What is its time and memory complexity and why.

What have I done before posting a question on SO ?

I have read this, this, this and many more links.

What I have done so far ?

As I understood from all articles and questions that I read, all my operations with loops are linear. I have 3 loops so it's N+N+N complexity and we can write it as N. I think that complexity of getData is O(n). Space complexity is more complex, as I understand it's often equal to time complexity for simple data structures so I think space complexity is also N but I have cache object (Hash Table) that save every response from fetchData, so I don't understand how to calculate it as space complexity.

Function

https://jsfiddle.net/30k42hrf/9/

or

const cache = {};
const fetchData = (key, arrayOfKeys) => {
  const result = [];
  for (let i = 0; i < arrayOfKeys.length; i++) {
    result.push({
      isin: arrayOfKeys[i],
      data: Math.random()
    });
  }
  return result;
}
const getData = (key, arrayOfKeys) => {
  if (arrayOfKeys.length < 1) return null;

  const result = [];
  const keysToFetch = [];

  for (let i = 0; i < arrayOfKeys.length; i++) {
    const isin = arrayOfKeys[i];
    if (key in cache && isin in cache[key]) {
      result.push({
        isin,
        data: cache[key][isin]
      });
    } else {
      keysToFetch.push(isin);
    }
  }

  if (keysToFetch.length > 0) {
    const response = fetchData(key, keysToFetch);

    for (let i = 0; i < response.length; i++) {
      const { isin, data } = response[i];
      if (cache[key]) {
        cache[key][isin] = data;
      } else {
        cache[key] = { [isin]: data }
      }
    }

    return [...result, ...response];
  }

  return result;
}

// getData('123', ['a', 'b'])

Thanks

nl pkr
  • 656
  • 1
  • 11
  • 21

1 Answers1

0

Time/Space complexity is determined in terms of how much more time/space will the iterations take as input increased/doubled. An intuitive view is that imagine the input size is 10, think about the time/space it takes and then think about it again as input size is 20, then input size is 100.

I am not quite clear about your code here but for a general cache stuff, the average time complexity is O(1) because once you got it in the cache, the retrieve time complexity is always O(1). You can think about a case that you retrieve the same item for 1 million times but you only need to store it once.

The average space complexity is O(n) because essentially you need to store everything in the space, which is N.

When it comes to extreme worst case, the time complexity can also be worse for the first time retrieving.

  • Ok, retrieve time complexity for cache is O(1) but if I iterate through all array (`n` size length) and put every data piece in cache (HashMap), before I `retrieve` data from cache, my time complexity is O(n + 1) which is O(n), right ? – nl pkr Oct 04 '18 at 23:03
  • Right, but you will not keep building your cache in reality (that is why we need cache), so the average would be O(1) if we retrieve enough times. – user2014622 Oct 04 '18 at 23:35
  • but as I read, you always need to look for the worst time complexity. So if I iterate through n-size array to put each data from array to cache the worst time complexity will be O(n). O(1) is only time complexity if I get (find) data in HashMap like once, but if I need to get data n-times from cache than time complexity will be O(n). Am I right ? – nl pkr Oct 05 '18 at 00:43
  • Your analysis is right, but we don't always check for the worst time complexity. For example, hashtable's lookup complexity is O(n) at worst case, but O(1) at average. Unless it is explicitly asked in an interview for worst complexity, we always count it as O(1) just like in reality. Like I said, once you had the cache built up, your complexity will be O(1), so assuming you have a system 90% of the traffic would hit cache and then no one would worry about it. – user2014622 Oct 05 '18 at 03:01
  • One more thing, in an industry software projects, O(n) complexity is already good enough, but O(n^2) or NP hard could be scarry because it doesn't scale. – user2014622 Oct 05 '18 at 03:03