Skip to main content

Posts

Showing posts from 2017

LRU Cache [HARD]

Company: Hike, Google, Goldman-Sachs, Flipkart, Expedia, Amazon, Adobe, Walmart-Labs, Ola-Cabs, Microsoft, MakeMyTrip, Intuit, Informatica, Yahoo. Question: The task is to design and implement methods of an  LRU cache . The class has two methods get and set which are defined as follows. get(x)   : Gets the value of the key x if the key exists in the cache otherwise returns -1 set(x,y) : inserts the value if the key x is not already present. If the cache reaches its capacity it should invalidate the least recently used item before inserting the new item. In the constructor of the class the size of the cache should be intitialized.   Input: The first line of input contain an integer T denoting the no of test cases. Then T test case follow. Each test case contains 3 lines. The first line of input contains an integer N denoting the capacity of the cache and then in the next line is an integer Q denoting the no of queries Then Q queries follow. A Query can be of two types 1. SET x y :

Topological Sort [MEDIUM] - DFS application-1

Given a directed graph you need to complete the function  topoSort  which returns an array having the topologically sorted elements of the array and takes two arguments . The first argument is the Graph graph  represented as adjacency list and the second is the number of vertices  N  . Company: OYO Rooms, Moonfrog Labs, Microsoft, Flipkart, Amazon,Accolite Note :  There can be multiple topological sorts of a Graph.  The driver program that calls your function doesn't match your output element by element, but checks whether the output produced by your function is a valid topological sort or not. Input: The first line of input takes the no of test cases then T test cases follow . Each test case contains two lines . The first  line of each test case  contains two integers E and N representing no of edges and the no of vertices . Then in the next line are E  pairs of integers u v representing an edge from u to v in the graph. Output: For each test case output will be 1 if the top

BFS: Shortest Reach in a Graph [HARD]

Consider an undirected graph consisting of   nodes where each node is labeled from   to   and the edge between any two nodes is always of length  . We define node   to be the starting position for a BFS. Given   queries in the form of a graph and some starting node,  , perform each query by calculating the shortest distance from starting node   to all the other nodes in the graph. Then print a single line of   space-separated integers listing node  's shortest distance to each of the   other nodes (ordered sequentially by node number); if   is disconnected from a node, print   as the distance to that node. Input Format The first line contains an integer,  , denoting the number of queries. The subsequent lines describe each query in the following format: The first line contains two space-separated integers describing the respective values of   (the number of nodes) and   (the number of edges) in the graph. Each line   of the   subsequent lines contains two spac