Skip to main content

Posts

Showing posts with the label Amazon

Element with left side smaller and right side greater- [EASY]

Given an unsorted array of size  N . Find the first element in array such that all of its left elements are smaller and all right elements to it are greater than it. Note:  Left and right side elements can be equal to required element. And extreme elements cannot be required element. Input: The first line of input contains an integer T denoting the number of test cases. Then T test cases follow. Each test case consists of two lines. First line of each test case contains an Integer N denoting size of array and the second line contains N space separated array elements. Output: For each test case, in a new line print the required element. If no such element present in array then print -1. Constraints: 1 <= T <= 100 3 <= N <= 10 6 1 <= A[i] <= 10 6 Example: Input: 3 4 4 2 5 7 3 11 9 12 6 4 3 2 7 8 9 Output: 5 -1 7 Explanation: Testcase 1 :  Elements on left of 5 are smaller than 5 and on right of it are greater than 5. Execution Ti...

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

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

Minimum Spanning Tree using Prim's algorithm [VERY IMPORTANT-Graphs]

CODE:  import java.util.*; import java.lang.*; import java.io.*; class GFG { public static void main (String[] args) { int graph[][] = new int[][] {{0, 2, 0, 6, 0},                                     {2, 0, 3, 8, 5},                                     {0, 3, 0, 0, 7},                                     {6, 8, 0, 0, 9},                                     {0, 5, 7, 9, 0},                                    };     primMST(graph);                       ...

Making Anagrams [BASIC/Cracking the coding interview ]

Company: Amazon Question: Alice is taking a cryptography class and finding  anagrams  to be very useful. We consider two strings to be anagrams of each other if the first string's letters can be rearranged to form the second string. In other words, both strings must contain the same exact letters in the same exact frequency For example,  bacdc  and  dcbac  are anagrams, but  bacdc  and  dcbad  are not. Alice decides on an encryption scheme involving two large strings where encryption is dependent on the minimum number of character deletions required to make the two strings anagrams. Can you help her find this number? Given two strings,   and  , that may or may not be of the same length, determine the minimum number of character deletions required to make   and   anagrams. Any characters can be deleted from either of the strings. Input Format The first line contains a single string,...