Interview Codes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

1.

/* Interview Related Codes */


2.
3. class LIS {
4. public:
5. vector<int> arr;
6. vector<int> lis;
7. LIS(vector<int> input){
8. arr = input;
9. }
10.
11. void computeLIS()
12. {
13. for(int i=0; i<arr.size(); i++){
14. if(lis.empty()) lis.push_back(arr[i]);
15. else {
16. int maxElement = lis[(int)lis.size()-1];
17. if(arr[i] > maxElement) lis.push_back(arr[i]);
18. else {
19. int pos = lower_bound(lis.begin(), lis.end(), arr[i]) - lis.begin();
20. lis[pos] = arr[i];
21. }
22. }
23. }
24. }
25.
26. vector<int> findLIS()
27. {
28. computeLIS();
29. return lis;
30. }
31.
32. };
33.
34. class maxHeap {
35. private:
36. vector<int> heap;
37. int findParent(int idx){
38. return (idx-1)/2;
39. }
40. int findLeft(int idx){
41. return 2*idx+1;
42. }
43. int findRight(int idx){
44. return 2*idx+2;
45. }
46. void heapifyUp(int idx){
47. if(idx && heap[findParent(idx)] < heap[idx]){
48. swap(heap[findParent(idx)], heap[idx]);
49. heapifyUp(findParent(idx));
50. }
51. }
52. void heapifyDown(int idx){
53. int left = findLeft(idx);
54. int right = findRight(idx);
55.
56. int valid = idx;
57. if(left < findSize() && heap[left] > heap[idx]) valid = left;
58. if(right < findSize() && heap[right] > heap[idx]) valid = right;
59.
60. if(valid != idx){
61. swap(heap[idx], heap[valid]);
62. heapifyDown(valid);
63. }
64. }
65. public:
66. unsigned int findSize(){
67. return (unsigned int) heap.size();
68. }
69. bool isEmpty(){
70. return (findSize() == 0);
71. }
72. void push(int val){
73. heap.push_back(val);
74. int idx = findSize() - 1;
75. heapifyUp(idx);
76. }
77. void pop(){
78. heap[0] = heap.back();
79. heap.pop_back();
80. heapifyDown(0);
81. }
82. int top(){
83. return heap[0];
84. }
85. void prnt(){
86. cout << "myHeap ";
87. for(int i=0; i<heap.size(); i++) cout << heap[i] << " ";
88. cout << endl;
89. }
90. };
91.
92. class rollingHash{
93. public:
94. #define ll long long
95. string text, pattern;
96. ll base, MOD;
97. rollingHash(string givenText, string givenPattern, ll givenBase, ll givenMod){
98. text = givenText;
99. pattern = givenPattern;
100. base = givenBase;
101. MOD = givenMod;
102. }
103. ll kLengthPrefixHash(string &str, int k)
104. {
105. ll power = 1;
106. ll ans = 0;
107. for(int i=k-1; i>=0; i--){
108. ans = ans + (power*(str[i]-'a'))%MOD;
109. ans = ans%MOD;
110. power = (power*base)%MOD;
111. }
112. return ans;
113. }
114. ll powerFunction(ll x, ll y)
115. {
116. ll ans = 1;
117. for(int i=1; i<=y; i++) ans = (ans*x)%MOD;
118. return ans;
119. }
120. vector<int> findPositionMatch()
121. {
122. int len = (int) pattern.length();
123. ll hashValue = kLengthPrefixHash(pattern, len);
124. ll rollingHash = kLengthPrefixHash(text, len);
125. vector<int> answer;
126. answer.push_back(0);
127. ll power = powerFunction(base, len-1);
128. for(int i=len; i<text.length(); i++){
129. int prev_char = text[i-len]-'a';
130. int cur_char = text[i]-'a';
131. rollingHash = (rollingHash - (prev_char * power)%MOD + MOD)%MOD;
132. rollingHash = ((rollingHash*base)%MOD + cur_char)%MOD;
133. if(hashValue == rollingHash) answer.push_back(i-len+1);
134. }
135. return answer;
136. }
137. };
138.
139. class quickSort{
140. public:
141. int partitionIdx(vector<int> &arr, int l, int r)
142. {
143. int idx = l+1;
144. int pivot = arr[l];
145. for(int j=l+1; j<=r; j++){
146. if(arr[j] < pivot){
147. swap(arr[idx], arr[j]);
148. idx++;
149. }
150. }
151. swap(arr[l], arr[idx-1]);
152. return idx-1;
153. }
154.
155. void quick_sort(vector<int> &arr, int l, int r)
156. {
157. if(l < r){
158. int idx = partitionIdx(arr, l, r);
159. quick_sort(arr, l, idx-1);
160. quick_sort(arr, idx+1, r);
161. }
162. }
163. };
164.
165. class KMP{
166. public:
167. vector<int> prefix_function(string str)
168. {
169. int n = (int) str.length();
170. vector<int> pi(n);
171. pi[0] = 0;
172. for(int i=1; i<n; i++){
173. int j = pi[i-1];
174. while(j > 0 && str[i] != str[j]){
175. j = pi[j-1];
176. }
177. if(str[i] == str[j]) ++j;
178. pi[i] = j;
179. }
180. return pi;
181. }
182. };
183.
184. struct Point{
185. int x, y;
186. };
187.
188. class geometry{
189. public:
190. // Given three colinear points p, q, r, the function checks
191. // if point q lies on the segment 'pr'
192. bool onSegment(Point p, Point q, Point r){
193. if(q.x >= min(p.x, r.x) && q.x <= max(p.x, r.x) &&
194. q.y >= min(p.y, r.y) && q.y <= max(p.y, r.y)){
195. return true;
196. }
197. else return false;
198. }
199. // To find orientation of ordered triplet (p1, p2, p3)
200. // The function returns the following values
201. // 0 -> p, q, r are collinear
202. // 1 -> clockwise
203. // 2 -> counter clockwise
204. int orientation(Point p1, Point p2, Point p3){
205. int val = (p2.y - p1.y) * (p3.x - p2.x) - (p2.x - p1.x) * (p3.y - p2.y);
206. if(val == 0) return 0;
207. return (val > 0) ? 1:2;
208. }
209. bool doIntersect(Point p1, Point q1, Point p2, Point q2)
210. {
211. int o1 = orientation(p1, q1, p2);
212. int o2 = orientation(p1, q1, q2);
213. int o3 = orientation(p2, q2, p1);
214. int o4 = orientation(p2, q2, q1);
215.
216. if(o1 == 0 && onSegment(p1, p2, q1)) return true;
217. if(o2 == 0 && onSegment(p1, q2, p1)) return true;
218. if(o3 == 0 && onSegment(p2, p1, q2)) return true;
219. if(o4 == 0 && onSegment(p2, q1, q2)) return true;
220. return false;
221. }
222. };
223.
224. class priorityQueueComparator{
225. public:
226. struct Person {
227. int age, height;
228. };
229.
230. struct customCompare{
231. bool operator()(Person p, Person q){
232. return p.age<q.age;
233. }
234. };
235. priority_queue<Person, vector<Person>, customCompare> pq;
236. };
237.
238. class operatorOverloading{
239. struct Person{
240. int age, height;
241. Person(int _age, int _height){
242. age = _age;
243. height = _height;
244. }
245. bool operator < (const Person &other){
246. return age < other.age;
247. }
248. };
249. };

You might also like