|
| 1 | +# AmazonProblems |
| 2 | +Practice problems from Amazon |
| 3 | + |
| 4 | + |
| 5 | +Questions: |
| 6 | +1.A string of alphanumeric is there. Find a string that starts with b and ends with 3 |
| 7 | + |
| 8 | +characters. |
| 9 | + |
| 10 | +2.There is a sorted array which is of very large size.In that all except one no. are |
| 11 | + |
| 12 | +repeated once.How to find that non repeated no. |
| 13 | + |
| 14 | +3.There are 2 linked lists.Those 2 lists are meeting at a point. How to find that |
| 15 | + |
| 16 | +meeting point. |
| 17 | + |
| 18 | +4. How do you convert a decimal number to its hexa-decimal equivalent.Give a C |
| 19 | + |
| 20 | +code to do the same |
| 21 | + |
| 22 | +5. Given an array all of whose elements are positive numbers, find the maximum |
| 23 | + |
| 24 | +sum of a subsequence with the constraint that no 2 numbers in the sequence |
| 25 | + |
| 26 | +should be adjacent in the array. |
| 27 | + |
| 28 | +Eg. |
| 29 | + |
| 30 | +i) 3 2 7 10 should return 13 (sum of 3 and 10) |
| 31 | + |
| 32 | +ii) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) |
| 33 | + |
| 34 | +6. Given a Binary Search Tree, write a program to print the kth smallest element |
| 35 | + |
| 36 | +without using any static/global variable. You can pass the value k to any function |
| 37 | + |
| 38 | +also. |
| 39 | + |
| 40 | +7.You are given some denominations of coins in an array (intdenom[])and infinite |
| 41 | + |
| 42 | +supply of all of them. Given an amount (int amount), find the minimum number of |
| 43 | + |
| 44 | +coins required to get the exact amount. What is the method called? |
| 45 | + |
| 46 | +8.Given an array of size n. It contains numbers in the range 1 to n. Each number is |
| 47 | + |
| 48 | +present at least once except for 1 number. Find the missing number. |
| 49 | + |
| 50 | +9.Given an array of size n. It contains numbers in the range 1 to n. Each number is |
| 51 | + |
| 52 | +present at least once except for 2 numbers. Find the missing numbers. |
| 53 | + |
| 54 | +10.Given an array of size n. It contains numbers in the range 1 to n. Find the |
| 55 | + |
| 56 | +numbers which aren't present. |
| 57 | + |
| 58 | +11.Given a string,find the first un-repeated character in it? Give some test cases |
| 59 | + |
| 60 | +12.You are given a dictionary of all valid words. You have the following 3 |
| 61 | + |
| 62 | +operations permitted on a word: |
| 63 | + |
| 64 | +a) Delete a character |
| 65 | + |
| 66 | +b) Insert a character |
| 67 | + |
| 68 | +c) Replace a character |
| 69 | + |
| 70 | +Now given two words - word1 and word2 - find the minimum number of steps |
| 71 | + |
| 72 | +required to convert word1 to word2. (one operation counts as 1 step.) |
| 73 | + |
| 74 | +13.Given a cube of size n*n*n (i.e made up of n^3 smaller cubes), find the number |
| 75 | + |
| 76 | +of smaller cubes on the surface. Extend this to k-dimension. |
| 77 | + |
| 78 | +14.What is a C array and illustrate the how is it different from a list. |
| 79 | + |
| 80 | +15.Write a function which takes as parameters one regular expression(only ? and |
| 81 | + |
| 82 | +* are the special characters) and a string and returns whether the string matched |
| 83 | + |
| 84 | +the regular expression. |
| 85 | + |
| 86 | +16.Find the second largest element in an array with minimum no of comparisons |
| 87 | + |
| 88 | +and give the minimum no of comparisons needed on an array of size N to do the |
| 89 | + |
| 90 | +same. |
| 91 | + |
| 92 | +17. Given an array of size n ,containing every element from 1 to n+1, except one. |
| 93 | + |
| 94 | +Find the missing element. |
| 95 | + |
| 96 | +18. Two trains enter at the opposite sides of a tunnel of length L with speeds 'V'. |
| 97 | + |
| 98 | +A particle enters the tunnel at the same time with a speed 'v' and it vibrates in |
| 99 | + |
| 100 | +the tunnel[i.e. if it reaches the end of the tunnel then it comes back]. What is the |
| 101 | + |
| 102 | +position of the particle by the time the 2 trains meet? |
| 103 | + |
| 104 | +19.Given an array of size n+1 which contains all the numbers from 1 to n.Find the |
| 105 | + |
| 106 | +number which is repeated in O(n) time .How do you proceed with the same with |
| 107 | + |
| 108 | +floating numbers from 0 to 1 instead of 1 to n? |
| 109 | + |
| 110 | +20. Design a data structure to represent the movement of a knight on a chess |
| 111 | + |
| 112 | +board |
| 113 | + |
| 114 | +21. Write an algorithm to traverse a knight covering all the squares on a |
| 115 | + |
| 116 | +chessboard starting at a particular point. |
| 117 | + |
| 118 | +22. What is wrong with this class, assuming that this is its complete interface? |
| 119 | + |
| 120 | +class C { |
| 121 | + |
| 122 | +char *p; |
| 123 | + |
| 124 | +public: |
| 125 | + |
| 126 | +C() { p = new char[64]; strcpy(p, "Hello world"); } |
| 127 | + |
| 128 | +~C() { delete p; } |
| 129 | + |
| 130 | +void foo() { cout<< "My ptr is: '" << p << "'" <<endl; } |
| 131 | + |
| 132 | +}; |
| 133 | + |
| 134 | +Since this has an overtly programmed destructor, the member wise semantics for |
| 135 | + |
| 136 | +destruction are not good enough; therefore, they are not good enough for copy |
| 137 | + |
| 138 | +and assignment either. But, the copy ctor and op= are not programmed, so we |
| 139 | + |
| 140 | +will have some serious trouble. |
| 141 | + |
| 142 | +Gradual hinting: what happens when we make a copy? [correct answer: pointer is |
| 143 | + |
| 144 | +copied]. Now, the original goes out of scope, what happens to the copy? [pointer |
| 145 | + |
| 146 | +dangles]. How would you fix it? |
| 147 | + |
| 148 | +[also, that delete p should be delete[ p since p was allocated with the array new] |
| 149 | + |
| 150 | +Assuming that swap() and copy construction are part of your interface for class C, |
| 151 | + |
| 152 | +what's the cookie-cutter pattern for operator= that uses them? |
| 153 | + |
| 154 | +answer: |
| 155 | + |
| 156 | +C& C:perator=(const C &rhs) { |
| 157 | + |
| 158 | +if (this != &rhs) { |
| 159 | + |
| 160 | +C tmp(rhs); |
| 161 | + |
| 162 | +this->swap(tmp); |
| 163 | + |
| 164 | +} |
| 165 | + |
| 166 | +return *this; |
| 167 | + |
| 168 | +} |
| 169 | + |
| 170 | +]] |
| 171 | + |
| 172 | +23. “given two lists write a function which returns a list which is the intersection |
| 173 | + |
| 174 | +of the two lists.the original lists should remain same. |
| 175 | + |
| 176 | +(Intersection – if first list is say,1,20 3,45 and second list is 3,24 ,45,90,68 then |
| 177 | + |
| 178 | +intersection should be 3,45 ) |
| 179 | + |
| 180 | +24. Given two nodes of a binary tree find the closest ancestor of the two nodes. |
| 181 | + |
| 182 | +Note:consider binary tree and binary search tree also. |
| 183 | + |
| 184 | +25. Given an array all of whose elements are positive numbers, find the maximum |
| 185 | + |
| 186 | +sum of a subsequence with the constraint that no 2 numbers in the sequence |
| 187 | + |
| 188 | +should be adjacent in the array. |
| 189 | + |
| 190 | +i) 3 2 7 10 should return 13 (sum of 3 and 10) |
| 191 | + |
| 192 | +ii) 3 2 5 10 7 should return 15 (sum of 3, 5 and 7) |
| 193 | + |
| 194 | +26. Given a Binary Search Tree, write a program to print the kth smallest element |
| 195 | + |
| 196 | +without using any static/global variable. You can’t pass the value k to any function |
| 197 | + |
| 198 | +also. |
| 199 | + |
| 200 | +27.Given an array of size n. It contains numbers in the range 1 to n. Each number |
| 201 | + |
| 202 | +is present at least once except for 2 numbers. Find the missing numbers. |
| 203 | + |
| 204 | +28.Given an array of size n. It contains numbers in the range 1 to n. Find the |
| 205 | + |
| 206 | +numbers which aren't present. |
| 207 | + |
| 208 | +29. How would you find the second largest element in an array using minimum no |
| 209 | + |
| 210 | +of comparisons? |
| 211 | + |
| 212 | +30. Write a C program for level order traversal of a tree? |
| 213 | + |
| 214 | +31. You are given: 3 types of vehicles: Motorbike, Car, and a special type of car for |
| 215 | + |
| 216 | +the handicapped. |
| 217 | + |
| 218 | +3 Types of parking: Motorbike parking, Car parking, handicapped car parking. |
| 219 | + |
| 220 | +Motorbikes and cars can only park in their designated parkings, while the |
| 221 | + |
| 222 | +handicapped cars can park either in their own parking or the regular car parking. |
| 223 | + |
| 224 | +How would you model this as classes? Explain your methods. |
| 225 | + |
| 226 | +32.Two tables emp(empid,name,deptid,sal) and dept(deptid,deptname) are |
| 227 | + |
| 228 | +there.write a query which displays empname,correspondingdeptname also |
| 229 | + |
| 230 | +display those employee names who donot belong to any dept. |
| 231 | + |
| 232 | +Display the employees whose salary is less than average salary. |
| 233 | + |
| 234 | +33. what is the output of the program |
| 235 | + |
| 236 | +main() |
| 237 | + |
| 238 | +{ |
| 239 | + |
| 240 | +int c=5; |
| 241 | + |
| 242 | +printf("%d %d %d",c,c<<2,c>> 2); |
| 243 | + |
| 244 | +} |
| 245 | + |
| 246 | +34. |
| 247 | + |
| 248 | +main() |
| 249 | + |
| 250 | +{ |
| 251 | + |
| 252 | +int a[8][10],c=0,i,j; |
| 253 | + |
| 254 | + for(i=0;i<10; |
| 255 | + |
| 256 | +i++) for(j=0; |
| 257 | + |
| 258 | +j<8;j++) a[j][i]=c++; |
| 259 | + |
| 260 | +printf("%d",a[3][6]); |
| 261 | + |
| 262 | +} |
| 263 | + |
| 264 | +35.What is the wrong in this program |
| 265 | + |
| 266 | +main() |
| 267 | + |
| 268 | +{ |
| 269 | + |
| 270 | +char *p,*q; |
| 271 | + |
| 272 | +p=(char *)malloc(25); |
| 273 | + |
| 274 | +q=(char*) malloc(25); |
| 275 | + |
| 276 | +strcpy(p,"amazon" ); |
| 277 | + |
| 278 | +strcpy(q,"hyd"); |
| 279 | + |
| 280 | +strcat(p,q); |
| 281 | + |
| 282 | +printf("%s",p); |
| 283 | + |
| 284 | +} |
| 285 | + |
| 286 | +36.write prefix and post fix notation for (a+b)*c-(d+e)^(f-g) |
| 287 | + |
| 288 | +37.what is the output of the program |
| 289 | + |
| 290 | +main() |
| 291 | + |
| 292 | +{ |
| 293 | + |
| 294 | +inti=5; |
| 295 | + |
| 296 | +printf("%d",fun(fun(fun(fun( fun(i)))))); |
| 297 | + |
| 298 | +} |
| 299 | + |
| 300 | +void fun(inti) |
| 301 | + |
| 302 | +{ if(i%2) return (i+(7*4)-(5/2)+(2*2)); |
| 303 | + |
| 304 | +else return (i+(17/5)-(34/15)+(5/2)); |
| 305 | + |
| 306 | +} |
| 307 | + |
| 308 | +38.When it is always true boolean fun |
| 309 | + |
| 310 | +(node *p) |
| 311 | + |
| 312 | +{ |
| 313 | + |
| 314 | +return ((p==null)||(p->next==null)|| (p->info<=p->next->info)&&( fun(p->next))); |
| 315 | + |
| 316 | +} |
| 317 | + |
| 318 | +a)when list is empty or has one node |
| 319 | + |
| 320 | +b)when the ele are sorted in non decreasing order |
| 321 | + |
| 322 | +c)when the ele are sorted in non increasing order |
| 323 | + |
| 324 | +39.what is x here (x&&!(x&(x-1))==1) |
| 325 | + |
| 326 | +a)x is always a prime |
| 327 | + |
| 328 | +b)x is a power of 2 |
| 329 | + |
| 330 | +c)x is even d)x is odd |
| 331 | + |
| 332 | +40.what is valid in cpp char *cp; const char *cpp; 1) cpp=cp; 2) cp=cpp; |
0 commit comments