Let's build Spotify's Recent Search functionality with LRU caching mechanism. Microsoft Interview question.   
Tags: computerscience, beginners, career, tutorial
Published: 2020-04-25 02:19:44

When we search a song on Spotify, all your recent searches are stored under recent searches. Let's see how can we build similar functionality.

<h2>Understanding the requirements </h2>

Let's see all of the functionalities of Spotify's Recent Search Component. 
1> Searching a song: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/z85s7thzqs7zhzdp2h9a.PNG)

When you search for a song, that song is listed under recent searches, since the list was empty, Coldplay was added first. 

2> Searching more songs: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/xf2k94qq199dsiorv1in.PNG)

When you search for more songs, the list grows and the songs are pushed on to the list. 

Let's fill up the list: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/o59vyan4th3bf2t8y1az.PNG)

<strong>Observations</strong> : When we search for something, it's added to the front of the list.  
 
3> Search song when the list is full: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/jyj09c2ldhd9r186crvz.PNG)

When we search and list is full, the <strong>first</strong> song ie Coldplay, which was propagated to the end of the list was removed from the list. 

Here we get the first glimpse of LRU cache. 
<strong>LRU = Least Recently Use</strong>. Since we initially searched for Coldplay but we didn't search for it again, it was propagated to the back of the list, making it <strong>least used</strong> component, and hence removed from the list. 

This is concept will get clear as we move along. 

4> Delete from the list: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/06n4lxzduqz40oon1z59.PNG)

Here we removed marshmallow from the list, but the relative ordering was maintained. 

5> Searching for a song which was already in the list: 
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/y08i9oqyrzt1mj49kq6r.PNG)

Here Alan Walker was on the list, but we searched for it, hence the Alan Walker card was <strong>moved</strong> to the front.

<strong>What's LRU and why is it useful? </strong>

First what's cache? 
A cache is basically storage that facilitates high-speed read and writes functionalities. 

What's a caching mechanism? 
A caching mechanism is a set of operations that would facilitate high-speed read and writes. Since cache size is limited it's important to use an optimal caching mechanism that will take full advantage of a limited cache and store data efficiently. 

You might be familiar with this : 

<img src="https://media.giphy.com/media/dZF5h27O1fkYUb5kyS/giphy.gif" alt="gif" />

<h1>What's LRU and why LRU?</h1> 
LRU is a caching mechanism which performs the following operations: 
1> Move the least used item out of the cache when the cache is full.
2> If an item is accessed frequently move it to the front to take advantage of fast read and write speeds.
3> Perform relevant delete and update operations on items.

The main constraint is to perform all operations in O(1) time. 

Since I feel that Spotify's example might be a bit confusing, consider this situation. 

Your crush is coming over and you want to impress her, so you decide to impress her with your culinary skills by baking pizza for her. So you decide to head to a market and buy ingredients for pizza along with eggs and milk for your breakfast. 

We shall consider the cart as our cache, which can hold 5 items. 

Let's see the operations we perform, please open the image in new tab and zoom in and go through different LRU operations:  
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/h6wxfef0ft2dponi5j3v.jpg)

Now you know what's LRU, why use it, what are its applications, let's code it!  

<h2> Requirements: Design a component / LRU that performs : </h2>
1> Add items in O(1).
2> Remove items when overflow in O(1).
3> Delete items in O(1).
4> Get items in O(1).
5> update items in O(1).

<h2> Data Structure </h2>
![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/9r3ouw1vaakagxxza1fm.png)

HashTable Seems like a good choice, but it's not a list, it doesn't maintain the order in between items. 

<strong> 💡 how about combining two Data structures? </strong> 
The only issue with HashTables is the ordering, so let's combine a DataStructure with HashTables to serve our purpose. 

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/u3pqpz0hdatspe3vrfj1.png)

As you might've guessed stack/queue/tree won't work great with hashtables. 

To make our lives a bit easier, instead of going with Singly Linked List, we shall use <strong>Doubly Linked List</strong>. So that our deletion operation is much faster. 

1> Let's Structure our Doubly Linked List: 
```javascript
class DLL {
      int key;        // stores the key which we will use to reference the item
      int val;        // stores the actual information
      DLL prev;       // Points towards previous item
      DLL next;       // points towards next item
}
```

2> Let's structure our cache: 
```javascript
Map<Integer,DLL> cache = new HashMap<>();
```
We shall create key -> Node mappings.

Since it's our custom Doubly Linked List, we shall create 2 Nodes, Head and Tail. These two nodes will help us to add and pop items.  

3> Let's structure our operations. 

Operations : 

movetohead is common among these: 
add() : create a node and move it to head
get() : check if the given key exists using hashtable and move it to head

delete is common among these: 
delete() : remove the node from its position and discard it
popTail(): remove the node at the tail and discard it

Tricky: 
update(): when we update a node, 
          1> we remove it from its position in the list.
          2> move it to head.   

![Alt Text](https://dev-to-uploads.s3.amazonaws.com/i/rxn5qu7tw69619rea2po.png)

Bit of visualization : 
<img src = "https://media.giphy.com/media/UVGflYKYs8U60wpWYE/giphy.gif" />
<h6>(sorry for bad quality, please recommend a good site for creating gifs")</h6>

First let's code the functions with the help of which we shall interact with our cache.

```javascript
// add a new item
public void add(int key,int value){
  DLL node = new DLL();
  node.key = key;
  node.val = value;
  this.moveToHead(node);
 }

// get a item
public int get(int key){
  DLL node = cache.get(key);
  if(node == null){
      return -1;
   }else{
    this.moveToHead(node);
    return node.val;
   }
}

// delete a item
public boolean delete(int key){
  DLL node = cache.get(key);
  if(node == null) return false;
  this.deleteNode(node);
  return true;
 }

//pop from tail
public void popTail(){
  DLL nodeToBeRemove = tail.prev;
  DLL newTail = nodeToBeRemove.prev;
  newTail.next = tail;
  tail.prev = newTail;
 }

//update an item
public boolean update(int key,int value){
  DLL node = cache.get(key);
  if(node == null){
      return false;
  }else{
    node.val = value;
    this.deleteNode(node);
    this.moveToHead(node);
    return true;
  }
}
  
```

Now let's code the functions which will facilitate working our cache.

```javascript
public void deleteNode(DLL node){
  DLL next = node.next;
  DLL prev = node.prev;
  next.prev = prev;
  prev.next = next;
 }

public void moveToHead(DLL node){
  DLL next = head.next;
  head.next = node;
  node.prev = head;
  node.next = next;
  next.prev = node;
 }

```

Now that we have all the required functions let's put everything together : 

```javascript
class LRUCache {
    class DLL{
        int val;
        int key;
        DLL next;
        DLL prev;
    }
    
    public void moveToHead(DLL node){
        DLL next = head.next;
        head.next = node;
        node.next = next;
        next.prev = node;
        node.prev = head;
    }
    
    public void deleteNode(DLL node){
        DLL prev = node.prev;
        DLL next = node.next;
        prev.next = next;
        next.prev = prev;
    }
    
    DLL head;
    DLL tail;
    Map<Integer,DLL> map;
    int count;
    int capacity;
    public LRUCache(int capacity) {
        map = new HashMap<>();
        this.count = 0;
        this.capacity = capacity;
        head = new DLL();
        tail = new DLL();
        head.prev = null;
        head.next = tail;
        tail.prev = head;
        tail.next = null;
    }
    
    public int get(int key) {
        DLL node = map.get(key);
        if(node == null) return -1;
        this.moveToHead(node);
        return node.val;
    }
    
    public void add(int key,int value){
      DLL node = cache.get(key);
      if(node != null){
          this.update(key,value);
      }else{
          node = new DLL();
          node.key = key;
          node.val = value;
          this.moveToHead(node);
    }
        
    public boolean delete(int key){
      DLL node = cache.get(key);
      if(node == null) return false;
      this.deleteNode(node);
      cache.remove(key);
      return true;
    }
        
    public void popTail(){
      DLL nodeToBeRemove = tail.prev;
      DLL newTail = nodeToBeRemove.prev;
      newTail.next = tail;
      tail.prev = newTail;
      cache.remove(nodeToBeRemove.key);
    }   
    
    public boolean update(int key,int value){
      DLL node = cache.get(key);
      if(node == null){
          return false;
      }else{
        node.val = value;
        this.deleteNode(node);
        this.moveToHead(node);
        return true;
      }
    }
}
```

That's it! That's the entire LRU implementation. Our version is modular and one can easily remove a functionality if it's not required.

Spotify's recent search component implements only add, delete, poptail. We implemented the rest for completeness.

I hope you like this explanation! And thanks a lot if you made it till here. 

If I have made any mistakes or you've any doubts, please do comment.