Skip to main content

Posts

Showing posts with the label Hike

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...

Level order traversal in spiral form [EASY]

Company: Microsoft, Housing.com, Hike, Flipkart, Amazon, Adobe, Accolite, Teradata, PayU, Ola-Cabs Question:  Write a function to print spiral order traversal of a tree. For below tree, function should print 1, 2, 3, 4, 5, 6, 7.     Input: The task is to complete the method which takes one argument, root of the Tree. The struct node has a data part which stores the data, pointer to left child and pointer to right child. There are multiple test cases. For each test case, this method will be called individually. Output: The function should print level order traversal in spiral form . Constraints: 1 <=T<= 30 1 <=Number of nodes<= 3000 1 <=Data of a node<= 1000 Example: Input: 2 2 1 2 R 1 3 L 4 10 20 L 10 30 R 20 40 L 20 60 R Output: 1 3 2 10 20 30 60 40 There are two test cases.  First case represents a tree with 3 nodes and 2 edges where root is 1, left child of 1 is 3 and right child of 1 is 2.   Second test case repr...

Egg Dropping Puzzle: DP [MEDIUM/VERY IMPORTANT]

Company: MakeMyTrip, Hike, Google, Goldman-Sachs, D-E-Shaw, Amazon, Philips, Oracle, Opera, Nearby, Mynta, MAQ-Software, VMWare, Unisys, Service Now, Samsung Question: The following is a description of the instance of this famous puzzle involving n=2 eggs and a building with k=36 floors. Suppose that we wish to know which stories in a 36-story building are safe to drop eggs from, and which will cause the eggs to break on landing. We make a few assumptions: …..An egg that survives a fall can be used again. …..A broken egg must be discarded. …..The effect of a fall is the same for all eggs. …..If an egg breaks when dropped, then it would break if dropped from a higher floor. …..If an egg survives a fall then it would survive a shorter fall. …..It is not ruled out that the first-floor windows break eggs, nor is it ruled out that the 36th-floor do not cause an egg to break. In this problem you have to find minimum trials to solve for n eggs and k floors. For more description...

Search in a Rotated Array [EASY]

Company: MakemyTrip, Intuit, Hike, Flipkart, De-Shaw, BankBazaar, Amazon, Adobe, TimesInternet, Snapdeal, Paytm, Microsoft. Question: Given a sorted and rotated array (rotated at some point)  A[ ] , and given an element  K , the task is to find the index of the given element K in the array A[ ]. The array has no duplicate elements. If the element does not exist in the array, print -1.   Input: The first line of the input contains an integer  T , depicting the total number of test cases. Then T test cases follow. Each test case consists of three lines. First line of each test case contains an integer  N  denoting the size of the given array. Second line of each test case contains N space separated integers denoting the elements of the array A[ ]. Third line of each test case contains an integer K   denoting the element to be searched in the array. Output: Corresponding to each test case, print in a new line, the index of the e...

Kadane Algorithm- Longest contiguous sub-array with maximum sum [MEDIUM]

Asked in : Metlife, Housing.com ,Hike, Flipkart, FactSet, D-E Shaw ,Amazon,Accolite, Snapdeal, Samsung ,PayU , Paytm ,Oracle, Ola,Microsoft, Morgan Stanley Question: Given an array containing both negative and positive integers. Find the contiguous sub-array with maximum sum. Input: The first line of input contains an integer T denoting the number of test cases. The description of T test cases follows. The first line of each test case contains a single integer N denoting the size of array. The second line contains N space-separated integers A1, A2, ..., AN denoting the elements of the array. Output: Print the maximum sum of the contiguous sub-array in a separate line for each test case. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 100 -100 ≤ A[i] <= 100 Example: Input 2 3 1 2 3 4 -1 -2 -3 -4 Output 6 -1 Code: import java.io.*; // Java program to print largest contiguous array sum import java.util.*; class Solution {     public static void main (Strin...