Skip to main content

Posts

Showing posts from July, 2017

Power of 2 [Easiest/One liner]

Company: Oracle, Microsoft, Mallow Technologies, Intel , FactSet, BankBazaar, Adobe, Samsung, Practo Question: Given a positive integer N, check if N is a power of 2. Input: The first line contains 'T' denoting the number of test cases. Then follows description of test cases. Each test case contains a single positive integer N. Output: Print "YES" if it is a power of 2 else "NO". (Without the double quotes) Constraints: 1<=T<=100 0<=N<=10^18 Example: Input : 2 1 98 Output : YES ​NO Explanation:  (2^0 == 1) Code: import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { //code Scanner sc= new Scanner(System.in); int m = sc.nextInt(); for(int i=0;i<m;i++){ System.out.println((Math.log(sc.nextBigInteger().doubleValue())/Math.log(2))%1<=.00001?"YES":"NO"); } } }

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 element found in the array.  If element is no

Microsoft Interview Technical question set with answers - SET I

Company : Microsoft Asked in : Technical Interviews 1) What is basic difference between 32 bit and 64 bit operating system? A) The terms are just refer to ways a CPU handles information. The main difference is 64 bit version handles large amount of RAM more effectively than a 32 bit. 4GB RAM is maximum usable memory the 32 bit version can handle and 64 bit, more than 4GB. 2) What is deadlock? What are the conditions of deadlock? Can you write a code to show deadlock. What is the Bankers Algorithm? A) Operating system is a resources allocator that can be allocated to one process at time. Now resources allocated to a process are not preemptable; this means that once a resource has been allocated to a process, there is no simple mechanism by which the system can take the resource back from the process unless the process voluntarily gives it up or the system administrator kills the process. This can lead to a situation called deadlock. Lets take an example. Suppose a process P1 requir

N Queen Problem [Hard]

Company: Visa, Twitter, MAQ-Software, Amdocs, Amazon, Accolite Question: The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens attack each other. Given an integer n, print all distinct solutions to the n-queens puzzle. Each solution contains distinct board configurations of the n-queens’ placement, where the solutions are a permutation of [1,2,3..n] in increasing order, here the number in the  ith  place denotes that the  ith -column queen is placed in the row with that number. For eg below figure represents a chessboard [3 1 4 2]. 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 denoting the size of the chessboard. Output: For each test case, output your solutions on one line where each solution is enclosed in square brackets '[', ']' separated by a space . The solutions are permutations of {1, 2, 3 …,  n } in increasing

Missing number in array [Easiest]

Companies: Paytm, Ola-Cabs, Microsoft, Intuit, Cisco, Amazon Given an array of size n-1 and given that there are numbers from 1 to n with one missing, the missing number is to be found. Input: The first line of input contains an integer T denoting the number of test cases. The first line of each test case is N,size of array. The second line of each test case contains N-1 input C[i],numbers in array. Output: Print the missing number in array. Constraints: 1 ≤ T ≤ 200 1 ≤ N ≤ 1000 1 ≤ C[i] ≤ 1000 Example: Input 2 5 1 2 3 5 10 1 2 3 4 5 6 7 8 10 Output 4 9 Algorithm: 1) Get the actual size of array (n in our solution) 2) Find the sum using the formula n*(n+1)/2 3) Subtract all elements of array from the above obtained sum to get ans  Code; import java.util.*; import java.lang.*; import java.io.*; class Solution { public static void main (String[] args) { //code Scanner sc = new Scanner (System.in); int m = sc.nextInt(); fo

Gerd Keiser Optical fiber communications

Amazon.in Widgets Recommended: We believe that efforts of the authors must be lauded and hence we recommend you to appreciate the same by purchasing the book.  You can find the link to buy the book at an additional discount  exclusively sponsored by extc resources at  Amazon.in here Still if one wants to have PDF version of this book, then click below: Click here to download Gerd Keiser Steps to follow: 1) Click on the above link 2) Wait for 3 secs and click on the link 3) Wait for 5 secs and click on skip ad NOTE: 1)We do require these ads to fund the site and provide books for free. Besides these are harmless and just require 5 secs waiting and nothing else. Please be patient for 5 secs and get your desired book for FREE!   Also note that Extc Resources donot support piracy and only links to the materials. Using of the material is subjected to copyright and we are not responsible or endorse the same. Users agree that any action on usage of material will be sole

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 (String[] args)

Combination Sum in Java (Medium) Recursive Programming

Asked in: Microsoft, Infosys, Amazon , Accolite ,  ABCQ Question; Given an array of integers A and a sum B, find all unique combinations in A where the sum is equal to B. ach number in A may only be used once in the combination. Note:    All numbers will be positive integers.    Elements in a combination (a1, a2, … , ak) must be in non-descending order. (ie, a1 ≤ a2 ≤ … ≤ ak).    The combinations themselves must be sorted in ascending order.    If there is no combination possible the print "Empty" (without qoutes). Example, Given A = 10,1,2,7,6,1,5 and B(sum) 8, A solution set is: [1, 7] [1, 2, 5] [2, 6] [1, 1, 6] Input: First is T , no of test cases. 1<=T<=500 Every test case has three lines. First line is N, size of array. 1<=N<=12 Second line contains N space seperated integers(x). 1<=x<=9. Third line is the sum B. 1<=B<=30.   Output: One line per test case, every subset enclosed in () and in every set intergers should be space s