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