Abstract Classes - Polymorphism C++
Problem Statement :
Abstract base classes in C++ can only be used as base classes. Thus, they are allowed to have virtual member functions without definitions. A cache is a component that stores data so future requests for that data can be served faster. The data stored in a cache might be the results of an earlier computation, or the duplicates of data stored elsewhere. A cache hit occurs when the requested data can be found in a cache, while a cache miss occurs when it cannot. Cache hits are served by reading data from the cache which is faster than recomputing a result or reading from a slower data store. Thus, the more requests that can be served from the cache, the faster the system performs. One of the popular cache replacement policies is: "least recently used" (LRU). It discards the least recently used items first. For example, if a cache with a capacity to store 5 keys has the following state(arranged from most recently used key to least recently used key) - 5 3 2 1 4 Now, If the next key comes as 1(which is a cache hit), then the cache state in the same order will be - 1 5 3 2 4 Now, If the next key comes as 6(which is a cache miss), then the cache state in the same order will be - 6 1 5 3 2 You can observe that 4 has been discarded because it was the least recently used key and since the capacity of cache is 5, it could not be retained in the cache any longer. Given an abstract base class Cache with member variables and functions: mp - Map the key to the node in the linked list cp - Capacity tail - Double linked list tail pointer head - Double linked list head pointer set() - Set/insert the value of the key, if present, otherwise add the key as the most recently used key. If the cache has reached its capacity, it should replace the least recently used key with a new key. get() - Get the value (will always be positive) of the key if the key exists in the cache, otherwise return -1. You have to write a class LRUCache which extends the class Cache and uses the member functions and variables to implement an LRU cache. Input Format First line of input will contain the N number of lines containing get or set commands followed by the capacity of the cache. The following N lines can either contain get or set commands. An input line starting with get will be followed by a key to be found in the cache. An input line starting with will be followed by the key and value respectively to be inserted/replaced in the cache. Constraints 1 <= N <= 500000 1 <= M <= 1000 1 <= key <= 20 1 <= value <= 2000 Output Format The code provided in the editor will use your derived class LRUCache to output the value whenever a get command is encountered.
Solution :
Solution in C :
class LRUCache : public Cache {
public:
LRUCache(size_t capacity)
{
cp = capacity;
tail = 0;
head = 0;
}
~LRUCache() {
while(tail) {
auto node = tail;
tail = tail->prev;
delete node;
}
}
// Set/insert the value off the key, if present, otherwise
// add the key as the most recently used key. If the cache
// has reached its capacity, it should replace the least
// recently used key with a new key.
void set(int key, int value) {
Node* node;
auto it = mp.find(key);
if(mp.end() == it) {
if(mp.size() < cp) {
node = new Node(key, value);
if(tail) {
tail->next = node;
node->prev = tail;
} else {
tail = node;
head = node;
}
} else {
node = tail;
mp.erase(tail->value);
}
mp.insert(make_pair(key, node));
} else {
node = it->second;
}
node->key = key;
node->value = value;
set_mru(node);
}
// Get the value (will always be positive) of the key
// if the key exists in the cache and then make that key
// as the most recently used key; otherwise, return -1.
int get(int key) {
auto it = mp.find(key);
if(mp.end() == it)
return -1;
auto node = it->second;
set_mru(node);
return node->value;
}
private:
void set_mru(Node* node) {
if(node->next) {
node->next->prev = node->prev;
}
if(node->prev) {
node->prev->next = node->next;
}
if(tail == node)
tail = node->prev;
node->prev = 0;
node->next = head;
head = node;
}
};
View More Similar Problems
Swap Nodes [Algo]
A binary tree is a tree which is characterized by one of the following properties: It can be empty (null). It contains a root node only. It contains a root node with a left subtree, a right subtree, or both. These subtrees are also binary trees. In-order traversal is performed as Traverse the left subtree. Visit root. Traverse the right subtree. For this in-order traversal, start from
View Solution →Kitty's Calculations on a Tree
Kitty has a tree, T , consisting of n nodes where each node is uniquely labeled from 1 to n . Her friend Alex gave her q sets, where each set contains k distinct nodes. Kitty needs to calculate the following expression on each set: where: { u ,v } denotes an unordered pair of nodes belonging to the set. dist(u , v) denotes the number of edges on the unique (shortest) path between nodes a
View Solution →Is This a Binary Search Tree?
For the purposes of this challenge, we define a binary tree to be a binary search tree with the following ordering requirements: The data value of every node in a node's left subtree is less than the data value of that node. The data value of every node in a node's right subtree is greater than the data value of that node. Given the root node of a binary tree, can you determine if it's also a
View Solution →Square-Ten Tree
The square-ten tree decomposition of an array is defined as follows: The lowest () level of the square-ten tree consists of single array elements in their natural order. The level (starting from ) of the square-ten tree consists of subsequent array subsegments of length in their natural order. Thus, the level contains subsegments of length , the level contains subsegments of length , the
View Solution →Balanced Forest
Greg has a tree of nodes containing integer data. He wants to insert a node with some non-zero integer value somewhere into the tree. His goal is to be able to cut two edges and have the values of each of the three new trees sum to the same amount. This is called a balanced forest. Being frugal, the data value he inserts should be minimal. Determine the minimal amount that a new node can have to a
View Solution →Jenny's Subtrees
Jenny loves experimenting with trees. Her favorite tree has n nodes connected by n - 1 edges, and each edge is ` unit in length. She wants to cut a subtree (i.e., a connected part of the original tree) of radius r from this tree by performing the following two steps: 1. Choose a node, x , from the tree. 2. Cut a subtree consisting of all nodes which are not further than r units from node x .
View Solution →