Skip to main content

Posts

Showing posts with the label InterviewBit

Minimum Lights to Activate | Interview

Problem: Description There is a corridor in a Jail which is N units long. Given an array A of size N . The i th index of this array is 0 if the light at i th position is faulty otherwise it is 1. All the lights are of specific power B which if is placed at position X , it can light the corridor from [ X-B+1, X+B-1] . Initially all lights are off. Return the minimum number of lights to be turned ON to light the whole corridor or -1 if the whole corridor cannot be lighted. Problem Constraints 1 <= N <= 1000 1 <= B <= 1000 Input Format First argument is an integer array A where A[i] is either 0 or 1. Second argument is an integer B. Output Format Return the minimum number of lights to be turned ON to light the whole corridor or -1 if the whole corridor cannot be lighted. Example Input Input 1: A = [ 0, 0, 1, 1, 1, 0, 0, 1]. B = 3 Input 2: A = [ 0, 0, 0, 1, 0]. B = 3 Example Output Output 1: 2 Output 2: -1 Example Explanation Explanation 1: In the first configuration, ...

Min Steps in Infinite Grid | InterviewBit

Problem: Description You are in an infinite 2D grid where you can move in any of the 8 directions (x,y) to (x-1, y-1), (x-1, y) , (x-1, y+1), (x , y-1), (x , y+1), (x+1, y-1), (x+1, y) , (x+1, y+1) You are given a sequence of points and the order in which you need to cover the points. . Give the minimum number of steps in which you can achieve it. You start from the first point. Input Format Given two integer arrays A and B, where A[i] is x coordinate and B[i] is y coordinate of ith point respectively. Output Format Return an Integer, i.e minimum number of steps. Example Input Input 1: A = [0, 1, 1] B = [0, 1, 2] Example Output Output 1: 2 Example Explanation Explanation 1: Given three points are: (0, 0), (1, 1) and (1, 2). It takes 1 step to move from (0, 0) to (1, 1). It takes one more step to move from (1, 1) to (1, 2). Solution:  int  Solution::coverPoints(vector< int > &A, vector< int > &B...

Largest Number | InterviewBit

Problem: Given a list of non negative integers, arrange them such that they form the largest number. For example: Given [3, 30, 34, 5, 9] , the largest formed number is 9534330 . Note: The result may be very large, so you need to return a string instead of an integer. Asked In: AMAZON GOLDMAN SACHS MICROSOFT Solution: static   bool  compareString(string a,string b){      return  a+b > b+a; } string Solution::largestNumber( const  vector< int > &A) {     vector<string> str;      for ( int  x:A)         str.push_back(to_string(x));     sort(str.begin(),str.end(),compareString);     string res;      bool  flag =  true ;      for (string x:str){          if (x ==...

Sort array with squares! | InterviewBit

Problem Description Given a sorted array A containing N integers both positive and negative. You need to create another array containing the squares of all the elements in A and return it in non-decreasing order. Try to this in  O(N)  time. Problem Constraints 1 <= N <= 10 5 . -10 3 <= A[i] <= 10 3 Input Format First and only argument is an integer array A . Output Format Return a integer array as described in the problem above. Example Input Input 1: A = [-6, -3, -1, 2, 4, 5] Input 2: A = [-5, -4, -2, 0, 1] Example Output Output 1: [1, 4, 9, 16, 25, 36] Output 2: [0, 1, 4, 16, 25] Solutions 1: vector< int > Solution::solve(vector< int > &A) {      int  n=A.size();      for ( int  i= 0 ;i<n;i++)         A[i] *= A[i];     sort(A.begin(),A.end());      return  A; } Solutions 2: vect...

Buffered Reader | InterviewBit

Problem Description The BufferedReader class of Java is used to read the stream of characters from the specified source (character-input stream). The constructor of this class accepts an InputStream object as a parameter This class provides a method named read() and readLine() which reads and returns the character and next line from the source (respectively) and returns them. Instantiate an  InputStreamReader  class bypassing your InputStream object as a parameter. Then, create a BufferedReader, bypassing the above obtained InputStreamReader object as a parameter. Now, read data from the current reader as String using the readLine() or read() method. For Example: Suppose we want to read an integer from the user then the code will look like: BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int toRead = Integer.parseInt(reader.readLine()); Another Example: Suppose we want to read a line (in form of a string) from the user then the code will loo...

Stdin and Stdout | InterviewBit

Problem Description Java input and output is an essential concept while working on java programming. It consists of elements such as input, output and stream. The input is the data that we give to the program. The output is the data what we receive from the program in the form of result. Stream represents flow of data or the sequence of data. To give input we use the input stream and to give output we use the output stream. One popular way to read input from stdin is by using the Scanner class and specifying the Input Stream as System.in. For example: Scanner scanner = new Scanner(System.in); String userString = scanner.next(); int userInt = scanner.nextInt(); scanner.close(); System.out.println("myString is: " + userString); System.out.println("myInt is: " + userInt); Similarly, we can use nextLong() , nextFloat() , nextDouble() methods to get long, float, double input respectively from the user. Similarly nextLine() method advances this scanner past the c...