Round 1:
1. Given a source array of integers with possible duplicates and a target integer, write algorithm to find out 2 numbers in source array whose sum is equal to target integer.
Algorithm:
SumToTarget (a[], ar_size, sum) 1) Sort the array in ascending order using merge sort for low complexity. 2) Initialize two index variables to find the candidate elements in the sorted array. (a) Initialize first to the leftmost index: first_index = 0 (b) Initialize second the rightmost index: last_index = ar_size-1 3) Loop while first_index < last_index. (a) If (A[first_index] + A[last_index] == sum) then return 1 (b) Else if( A[first_index] + A[last_index] < sum ) then last_index++ (c) Else first_index-- 4) If no candidates in whole array - return 0
Implementation :-
import java.util.*; import java.lang.*; class Main { public static void main (String[] args) throws java.lang.Exception { int a[]={4,1,2,7,9,3}; int length=a.length; length--; mergesort(a,0,length); if(SumToTarget(a,0,length,4)==1) System.out.println("Duplicates exist in the array"); else System.out.println("Duplicates does not exist in the array"); } /* Function to check whether the duplicates whose sum equal to target num exist or not */ public static int SumToTarget(int a[],int start,int last , int num) { int first_index=start; int last_index=last; /*if first_index becomes greater than last_index it means duplicates does not exist */ while(first_index<last_index) { /* if sum of dupliactes is equal to required sum it means duplicates are present hence returning 1 */ if((a[first_index]+a[last_index])==num ) { System.out.println("Two numbers are:->"+a[first_index]+" "+a[last_index]); return 1; } /*if sum of two number is greater than the required sum then the last_index will be decreased*/ else if((a[first_index]+a[last_index])>num) { last_index--; } /*if sum of two number is less than the required sum then the first_index will be increased*/ else if((a[first_index]+a[last_index])<num) { first_index++; } } /*program will reach here only if there no duplicates equal to sum consequently will return 0 */ return 0; } public static void mergesort(int a[],int start,int last) { if(start<last) { int mid=(start+last)/2; mergesort(a,start,mid); mergesort(a,mid+1,last); merge(a,start,last,mid); } } public static void merge(int a[],int start ,int last,int mid ) { int j,k,l; int c[]=new int[a.length]; j=start; k=start; l=mid+1; while(j<=mid && l<=last ) { if(a[j]<a[l]) { c[k]=a[j]; k++; j++; } else { c[k]=a[l]; k++; l++; } } while(j<=mid) { c[k]=a[j]; k++; j++; } while(l<=last) { c[k]=a[l]; k++; l++; } for(int i=start;i<=k-1;i++) { a[i]=c[i]; } } }
Running program:-http://ideone.com/YE0d2I
Analysis:-
As we know that time complexity of mergesort is O(nlogn) and time complexity of sumToTarget function is O(n) resulting:-
O(nlogn)+O(n)=O(nlogn)
since according to the rule bigger time complexity is considered .
Time Complexity :- O(nlogn)
2. Say you have three tables WORK, USERS, MANAGERS
WORK- work_id, user_id, how_muchUSERS
USERS- user_id, teamMANAGERS
MANAGERS- manager_id, team
If I am a manager, write a select statement to retrieve the work of all users who belong to my team. The mapping of user to team and team to manager are defined in the USERS and MANAGERS table.
Ans:-
Select how_much from WORK, USERS,MANAGER where
WORK.user_id=USERS.user_id and USERS.team=MANAGERS.team
3. In a Chrome extension, which file contains the most important
information of the extension like version, pattern matches, etc.
"name": "My
Extension",
"version": "2.1",
"description": "Gets
information from Google.",
"icons": { "128": "icon_128.png" },
"background": {
"persistent": false,
"scripts": ["bg.js"]
}
For
more information of the answer of this question see the link:- http://developer.chrome.com/extensions/overview.html
4. There are three rooms, and there are Princess, Flowers and
Snake in those rooms. The doors of all the rooms have incorrect nameplates.
i.e., the nameplate for the princess’ room is not Princess. Similarly, the
nameplate for the Flowers’ room is not Flowers. You need to find the room of
the Princess without going to the room of Snake. How do you find?
5. Which is faster: finding an item in a hashtable or in a sorted
list? And Why?
6. How do you find out if a number is a power of 2? And how do you know if it is an odd number? Write code in the language of your choice.
Ans:- To check whether a number is a power of 2 we apply the and
operation between number and number-1 if we get 0 then it is otherwise it is
not .
for eg:-
To check whethera numbers is odd we didvide it by 2 if leaves remainder then it
is odd otherwise it is even.
7.Number of users in 2010 for adwords, chrome, gmail, android and picassa are in the ratio of 6:9:14:13:8, and in 2011 we add 3 lakh more users. New ratio is 8:12:13:11:6. Number of picassa users in 2011= 1,44,000. Calculate number of adwords users in 2010.
Please comment if you like the above post or if you find mistake .
Thank you very much bro..! I was looking for solutions and i got it through your blog post..! thank you for uploading the interview solutions on internet
ReplyDeleteI am glad pranay you like it.
ReplyDeleteCan u tell me how did you get 6/50 ?
ReplyDelete