Skip to main content

Posts

Showing posts with the label Yahoo

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

Return two primes [HARD/IMP] (Goldbach’s conjecture)

Company: Zoho, Yahoo Question: Given an even number ( greater than 2 ), return two prime numbers whose sum will be equal to given number. There are several combinations possible. Print only first such pair.  NOTE:  A solution will always exist, read  Goldbach’s conjecture . Also, solve the problem in linear time complexity, i.e., O(n). Input: The first line contains T, the number of test cases. The following T lines consist of a number each, for which we'll find two prime numbers. Note: The number would always be an even number.   Output: For every test case print two prime numbers space separated, such that the smaller number appears first. Answer for each test case must be in a new line. Constraints: 1 ≤ T ≤ 70 1 ≤ N ≤ 10000 Example: Input: 5 74 1024 66  8 9990 Output: 3 71 3 1021 5 61 3 5 17 9973 CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (Str...

Largest Prime Factor [MEDIUM]

Company: Yahoo Question: Given a no N, the task is to find the largest prime factor of the number. Input: The first line of input contains an integer T, denoting the no of test cases. Then T test cases follow. Each test case contains an integer N. Output: For each test case in a new line print the largest prime factor of N. Constraints: 1<=T<=100 2<=N<=10^11+1 Example: Input: 2 6 15 Output: 3 5 CODE: import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { Scanner sc = new Scanner (System.in); int m = sc.nextInt(); for(int mi=0;mi<m;mi++){     int n = sc.nextInt();     lpf(n); } } public static void lpf(int n ){     int num=0;     while(n%2==0){         n/=2;        }        if(n%2==0){num=2;}          for(int i=3;i<Mat...

Word Boggle [MEDIUM]

Company: Microsoft, MakeMyTrip, Google, Facebook, Directi, Amazon, Yahoo, Nvidia. Question: Given a dictionary, a method to do lookup in dictionary and a M x N board where every cell has one character. Find all possible words that can be formed by a sequence of adjacent characters. Note that we can move to any of 8 adjacent characters, but a word should not have multiple instances of same cell. Example: Input: dictionary[] = {"GEEKS", "FOR", "QUIZ", "GO"}; boggle[][] = {{'G','I','Z'}, {'U','E','K'}, {'Q','S','E'}}; Output: Following words of dictionary are present GEEKS, QUIZ Input: The first line of input contains an integer T denoting the no of test cases . Then T test cases follow. Each test case contains an integer x denoting the no of words in the dictionary. Then in the next line are x space...