Search in rotational sorted array[ Microsoft – Amazon – FlipKart ]

Problem: Find a value from a rotational sorted array. Solution Hint: binary search. Link #include <stdio.h> int arr[100010]; int bin_search(int lw,int hi,int v) { if(v<arr[lw] || v>arr[hi]) return 0; int mid; while(lw<=hi) { mid=(lw+hi)/2; if(arr[mid]==v) { printf("%d\n",mid); return 1; } else if(arr[mid]<v) lw=mid+1; else if(arr[mid]>v) hi=mid; } return 0; } int main() { //code int t,i,j,n,val,a,idx;

Traverse 4×4 matrix spirally.[ Microsoft – BrowserStack ]

Problem:Traverse 4×4 matrix spirally. Link #include <stdio.h> int main() { //code int t,i,j; int ar[4][4]; scanf("%d",&t); while(t–) { for(i=0;i<4;i++) { for(j=0;j<4;j++) scanf("%d",&ar[i][j]); } for(i=0;i<4;i++) printf("%d ",ar[0][i]); for(i=1;i<4;i++) printf("%d ",ar[i][3]); for(i=2;i>=0;i–) printf("%d ",ar[3][i]); printf("%d %d ",ar[2][0],ar[1][0]); printf("%d %d ",ar[1][1],ar[1][2]); printf("%d %d\n",ar[2][2],ar[2][1]); } return 0; }  

Merge two Sorted (non-increasing) Array.[ Microsoft – quikr – LinkedIn – Snapdeal ]

Problem:Merge two Sorted (non-increasing) Array. Link #include <stdio.h> void _sort(int ar[],int len1,int br[],int len2) { int i=1,j=1; while(i<=len1 && j<=len2) { if(ar[i]>br[j]) { printf("%d ",ar[i]); i+=1; } else if(ar[i]<br[j]) { printf("%d ",br[j]); j+=1; } else if(ar[i]==br[j]) { printf("%d %d ",ar[i],br[j]); j+=1; i+=1; } } while(i<=len1) printf("%d ",ar[i++]); while(j<=len2) printf("%d ",br[j++]); printf("\n"); } int main() {

Find Excel Column Name From Column Number [ Microsoft, Amazon, Samsung ]

Problem:Given a positive integer, return its corresponding column title as appear in an Excel sheet. MS Excel columns has a pattern like A, B, C, … ,Z, AA, AB, AC,…. ,AZ, BA, BB, … ZZ, AAA, AAB ….. etc. In other words, column 1 is named as “A”, column 2 as “B”, column 27 as

Parenthesis Checker [ Snapdel – Amazon ]

Problem: Check the balance of parenthesis sequence, the string contains (),{} and []. Link #include <stdio.h> #include <string.h> int main() { //code char s[105]; char arr[105]; int balance; int t,idx,i; scanf("%d",&t); while(t–) { scanf("%s",s); idx=0; balance=1; for(i=0;i<strlen(s);i++) { if(s[i]=='(' || s[i]=='{' || s[i]=='[') arr[idx++]=s[i]; else if(s[i]==')') { if(arr[idx-1]=='(') { idx–; } else balance=0; } else

Find Maximum Distance [ Google-Amazon ]

Problem: An array of integers will be given, find the maximum distance of indexes of  [j – i]  subjected to the constraint of A[i] <= A[j]. A : [4 6 5 3]; Output : 2; For the pair (4, 5). Link #include <stdio.h> int arr[1005]; int t,n,mx; int main() { //code int i,j; scanf("%d",&t); while(t–) { scanf("%d",&n); for(

Removing duplicate chars[Microsoft]

Problem: Given a string, Remove all the duplicate chars, string may include spaces. Solution Hints:solN; ASCII char valaus range from -128 to 127. Link The following solution did not get AC from OJ. My guess is, there may be a problem with that OJ. #include <iostream> #include <string> #include <stdio.h> using namespace std; int main() { //code int

Find Largest Number formed from an Array [PayTm , Amazon]

Problem:A list of non negative integers will be provided, arrange the numbers in such a way that they form the largest number possible. Example: From a list : 9 30 31 4 10 ; Largest number formed : 94313010. Link #include <stdio.h> #include <algorithm> #include <vector> #include <iostream> #include <string> using namespace std; //vector<string> vb; string vb[105]; int n; bool