-
Notifications
You must be signed in to change notification settings - Fork 1
/
Interview Experience Problems.txt
1861 lines (1535 loc) · 58.4 KB
/
Interview Experience Problems.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
Problems which were asked in Product based companies.
1. Check if removing an edge can split a binary tree into two equal size trees
-> Asked in (Amazon)
Link: https://www.techiedelight.com/split-binary-tree-into-two-trees/
M1) O(n*n)
-> We firstly check the no of nodes, if even then its ok if odd return false.
-> Then we can a function which at every node checks that (2*size(root)==n)
-> If yes then it will return true. we can find this node in any part.
-> At last we return (check from left)||(check from right).
M2) O(n)
-> Here also the no of nodes should be even and after that
-> we are reducing the time complexity by just returning the size as we need it.
// Update `result` to true if the size of a binary tree rooted at `root`
// or the size of any of its subtrees is exactly `n/2`
public static int checkSize(Node root, int n, AtomicBoolean result)
{
// base case: an empty tree or result already found
if (root == null || result.get()) {
return 0;
}
// check if the size of a binary tree rooted at `root` is exactly `n/2` and
// update the result
int size = 1 + checkSize(root.left, n, result) +
checkSize(root.right, n, result);
if (!result.get()) {
result.set(2 * size == n);
}
return size;
}
---------------------------------------------------------->
2. Find Equilibrium index of array (Arr1 + Arr2 ... Arri-1 = Arri+1 + Arri+2 ... Arrn)
-> Asked in (Amazon)
O(n)
String equilibrium(int arr[], int n) {
int lsum=0;
int sum=0;
for(int val:arr)sum+=val;
for(int i=0;i<n;i++){
sum-=arr[i];
if(lsum==sum){
return "YES";// basically i is the index
}
lsum+=arr[i];
}
return "NO";
}
---------------------------------------------------------->
3. Max sum in the configuration (max sum of all configuration of rotations)
-> Asked in (Amazon)
-> So here we are firstly calculating array sum and first rotation sum,
-> After that the sum of next rotation can be calculated using the previous one
-> Since the current element will be at last so it has be to multiplied by (n-1).
-> Other terms index decreases by one so for that case
-> We are removing the whole array sum and for the last term since it was not there earlier (index=0),so it got
substracted extra, so for now we will multiply the curr term (last term for next rotation) with n instead of n-1.
int max_sum(int arr[], int n)
{
int allSum = 0;
int arrSum = 0;
for(int i=0;i<n;i++){
arrSum+=arr[i];
allSum+=arr[i]*i;
}
int max=allSum;
int currSum=allSum;
for(int i=0;i<n;i++){
currSum = currSum - arrSum + arr[i]*n;
max = Math.max(max,currSum);
}
return max;
}
---------------------------------------------------------->
4. Kth Smallest element in the BST
-> Asked in (Amazon)
-> O(h), S(h)
-> Here, lcount is in node structure, and it represents the no of nodes in the left subtree of the current Node.
// Function to find k'th largest element in BST
// Here count denotes the number of
// nodes processed so far
public static Node kthSmallest(Node root, int k)
{
// base case
if (root == null)
return null;
int count = root.lCount + 1;
if (count == k)
return root;
if (count > k)
return kthSmallest(root.left, k);
// else search in right subtree
return kthSmallest(root.right, k - count);
}
---------------------------------------------------------->
5. Distribute n candies among k people
-> Asked in (Amazon)
M1) O(sqrt(candies) + n)
public int[] distributeCandies(int candies, int num) {
int[]res = new int[num];
for(int i=0;candies>0;i++){
res[i%num]+=Math.min(candies,i+1);
candies-=i+1;
}
return res;
}
M2) -> https://leetcode.com/problems/distribute-candies-to-people/discuss/324215/Java-O(num_people)-one-pass-Math-Solution-solving-quadratic-function
---------------------------------------------------------->
6. Given a matrix of ‘O’ and ‘X’, replace ‘O’ with ‘X’ if surrounded by ‘X’
-> Asked in (Amazon)
-> So here we first replace all the ‘O’ with ‘-’
-> Then for a) 1st col every row b) last col every row c) 1st row every col d) last row every col
-> We will apply flood fill algo where prevV = ‘-’ & newV = ‘O’.
-> At last replace the remaining ‘-’ with ‘X’.
Intuition -> we are basically applying dfs to each boundry since problem says that when ‘O’ is surrounded with ‘X’ in all directions then only replace it with ‘X’. and boundry is the place we are sure for that it is not surrounded and for the places where it does not reach because of surroundings we then replace it with ‘X’.
static void floodFillUtil(char mat[][], int x,
int y, char prevV,
char newV)
{
// Base cases
if (x < 0 || x >= M ||
y < 0 || y >= N)
return;
if (mat[x][y] != prevV)
return;
// Replace the color at (x, y)
mat[x][y] = newV;
// Recur for north,
// east, south and west
floodFillUtil(mat, x + 1, y,
prevV, newV);
floodFillUtil(mat, x - 1, y,
prevV, newV);
floodFillUtil(mat, x, y + 1,
prevV, newV);
floodFillUtil(mat, x, y - 1,
prevV, newV);
}
---------------------------------------------------------->
7. Find Minimum Insertions To Form A Palindrome
-> Asked in (Amazon)
M1) Using LCS
-> Here just find longest common subsequence of (str, reverse of str)
-> Ans = str.length() - LCS(str,rev(str));
M2)
-> min insertions in string from [l....h] is
a) If (l==h) then 0 + minIns(str,l+1,h-1);
b) If (l!=h) then return Math.min( minIns(str,l+1,h), minIns(str,l,h-1) + 1.
static int findMinInsertions(char str[],
int l, int h,int[][]dp)
{
// Base Cases
if (l > h)
return Integer.MAX_VALUE;
if (l == h)
return 0;
if (l == h - 1)
return (str[l] == str[h]) ? 0 : 1;
if(dp[l][h]!=-1)return dp[l][h];
int a = 0;
if(str[l]==str[h]){
a = findMinInsertions(str,l+1,h-1,dp);
}else{
int x = findMinInsertions(str,l+1,h,dp);
int y = findMinInsertions(str,l,h-1,dp);
a = Math.min(x,y)+1;
}
return dp[l][h]=a;
}
---------------------------------------------------------->
8. Pairs of Songs With Total Durations Divisible by 60 (LC-1010)
-> Asked in (Amazon)
M1) Here we used combinations.
public int numPairsDivisibleBy60(int[] time) {
if(time == null || time.length == 0) return 0;
int n = time.length;
int[] map = new int[60];
int res = 0;
for(int i = 0; i < n; i++){
int remainder = time[i] % 60;
map[remainder]++;
}
res += (int)((long)map[0] * (map[0] - 1) / 2);
res += (int)((long)map[30] * (map[30] - 1) / 2);
for(int i = 1; i < 30; i++){
res += map[i] * map[60 - i];
}
return (int)res;
}
M2) Just Like Two sum problem
public int numPairsDivisibleBy60(int[] time) {
int remainder[]=new int[60];
int n=time.length;
int ans=0;
for(int i=0;i<n;i++){
int rem=time[i]%60;
if(rem==0)
ans+=remainder[0];
else
ans+=remainder[60-rem];
remainder[rem]++; //since we are increasing count of elements in this loop itself so //there will be no duplicates as we are not the frequencies beforehand.
}
return ans;
}
---------------------------------------------------------->
9. Extract leaves of a binary tree in a doubly linkedList
-> Asked in (Amazon)
->https://www.codingninjas.com/codestudio/library/extract-leaves-of-a-binary-tree-in-a-doubly-linked-list
-> Approach-> we will take a head of doubly linkedlist and now if we encounter a leaf node.
-> Then we just do node.right = head, head.left = node, head = node.
-> That's why we will traverse first right and then left.
-> And if you want to extract this leaf doubly linkedlist from the whole tree. just return null;
-> node.right = solve(node.right), node.left = solve(node.left);
---------------------------------------------------------->
10. Check if two nodes in a Binary tree are cousins or not
(cousin are those nodes who have same levels but different parents)
-> Asked in (Amazon)
-> find level of both
-> find if they are sibling or not.
-> finally mix both above conditions and return the result.
boolean isSibling(Node node, Node a, Node b)
{
// Base case
if (node == null)
return false;
return ((node.left == a && node.right == b) ||
(node.left == b && node.right == a) ||
isSibling(node.left, a, b) ||
isSibling(node.right, a, b));
}
int level(Node node, Node ptr, int lev( lev = 1))
{
// base cases
if (node == null)
return 0;
if (node == ptr)
return lev;
// Return level if Node is present in left subtree
int l = level(node.left, ptr, lev + 1);
if (l != 0)
return l;
// Else search in right subtree
return level(node.right, ptr, lev + 1);
}
M2)
-> We can do a level order traversal of the given nodes.
-> While doing the level order traversal, we would be using a queue of pair to store the given nodes along with its parents.
-> While traversing a level, if we find both the levels, we need to check their parents and if they are different, they are cousins.
-> If the nodes are present at different levels, they are not cousins.
---------------------------------------------------------->
11. Check if a given node is present in the complete binary tree index (1-n in level order wise)
(means if we travel level order wise then we get sorted array)
-> Asked in (Amazon)
-> O(LogN)
-> In a complete binary tree, except root, all the odd nodes are right child. and even nodes are left.
(condition is that the complete binary tree must be indexed from 1 - n level order wise)
// Function to store the
// list of the directions
public static ArrayList<Integer> findSteps(Node root, int data) {
ArrayList<Integer> steps = new ArrayList<Integer>();
// While the root is not found
while (data != 1) {
// If it is the right
// child of its parent
if (data / 2.0 > data / 2) {
// Add right step (1)
steps.add(1);
} else {
// Add left step (-1)
steps.add(-1);
}
// int parent = data / 2;
// Repeat the same
// for its parent
data = data / 2;
}
// Reverse the steps list
Collections.reverse(steps);
// Return the steps
return steps;
}
// Function to find
// if the key is present or not
public static boolean findKey(Node root, int data) {
// Get the steps to be followed
ArrayList<Integer> steps = findSteps(root, data);
// Taking one step at a time
for (Integer cur_step : steps) {
// Go left
if (cur_step == -1) {
// If left child does
// not exist return false
if (root.left == null)
return false;
root = root.left;
}
// Go right
else {
// If right child does
// not exist return false
if (root.right == null)
return false;
root = root.right;
}
}
// If the entire array of steps
// has been successfully
// traversed
return true;
}
---------------------------------------------------------->
12. Subarrays with K Different Integers (LeetCode 992)
-> Asked in (Amazon)
-> exact(k) = atmost(k) - atmost(k-1).
public int atmost(int[]nums,int k){
HashMap<Integer,Integer>map = new HashMap<>();
int i=0,j=0;
int result=0;
while(j<nums.length){
int val = nums[j];
map.put(val,map.getOrDefault(val,0)+1);
j++;
while(map.size()>k && i<j){
int temp = map.get(nums[i]);
if(temp==1){
map.remove(nums[i]);
}else{
map.put(nums[i],map.get(nums[i])-1);
}
i++;
}
result += j-i+1;
}
return result;
}
Similar problem ->
# Longest Substring with at most k distinct characters
---------------------------------------------------------->
13. Populating Next Right Pointers in Each Node (LeetCode 116)
-> Asked in (Amazon)
-> In this question the tree is perfect binary tree.
M1) We can use Level order traversal using Queue and for a given level just place next pointer for each node.
-> O(N) , S(N)
M2) Use of Recursion O(N) but S(1) -> only stack space used
public Node connect(Node root) {
dfs(root, null);
return root;
}
private void dfs(Node curr, Node next) {
if (curr == null) return;
curr.next = next;
dfs(curr.left, curr.right);
dfs(curr.right, curr.next == null ? null : curr.next.left);
}
M3) Just traverse and make linkedlist at each level (just dry run the code to understand better)
-> O(N), S(1)
public Node connect(Node root) {
Node level_start = root;
while(level_start!=null){
Node curr = level_start;
while(curr!=null){
//this makes the link from left subtree to right subtree
if(curr.left!=null) curr.left.next = curr.right;
//Although we need not to check curr.right because it is a perfect binary tree.
//through this we go to right side.
if(curr.right!=null && curr.next!=null) curr.right.next = curr.next.left;
curr = curr.next; //now going to right subtree
}
level_start = level_start.left; //since we link every level starting from extreme left.
}
return root;
}
##) Populating Next Right Pointers in Each Node-ii (LC-117)
S(1)
public Node connect(Node root) {
Node real_root = root;
Node temp = new Node(0);
Node pre = temp;
while(root!=null){
if(root.left!=null){
pre.next = root.left;
pre = pre.next;
}
if(root.right!=null){
pre.next = root.right;
pre = pre.next;
}
root = root.next;
if(root==null){
pre = temp;
root = temp.next;
temp.next=null; // to avoid TLE (because of loop in the last case, just dry run to understand.)
}
}
return real_root;
}
---------------------------------------------------------->
14. Find height of a special binary tree whose leaf nodes are connected in a circular doubly linkedlist
-> Asked in (Amazon)
-> Approach is simple just find the height as usual
-> When you get node == null return 0;
-> when you return leaf just return 1 height;
-> return 1 + Math.max(h(node.left),h(node.right));
-> Though below we need not to check for both left and right because it is a doubly linkedlist
static boolean isLeaf(Node node)
{
// If given node's left's right is pointing to given
// node and its right's left is pointing to the node
// itself then it's a leaf
return (node.left != null && node.left.right == node
&& node.right != null
&& node.right.left == node);
}
---------------------------------------------------------->
15. No. of Connected components in a graph using DSU
-> Asked in (Amazon)
// DSU with all the cases
// 1. Without Path compression -> O(M*N)
// 2. With Path compression but without union by rank -> O(N + M*LogN)
// 3. With Path compression and with union by Rank -> O( N + M * (amortized
// LogN)) => O(N+M)
public static int noOfConnectedComponents(int vertices, int[][] edges) {
n = vertices;
makeSet(); // this will make the parent and rank array
for (int[] edge : edges) {
union(edge[0], edge[1]);
}
Set<Integer> set = new HashSet<>();
for (int i = 0; i < vertices; i++) {
set.add(findPar(i));
}
return set.size();
}
---------------------------------------------------------->
16. Car Pooling (LeetCode 1094) / Meeting Rooms ii (LC- 253)
-> Asked in Amazon
M1) O(N) S(N)
public boolean carPooling(int[][] trips, int capacity) {
int[]prefix=new int[1001];
for(int i=0;i<trips.length;i++){
int numP=trips[i][0];
int from=trips[i][1];
int to=trips[i][2];
prefix[from]+=numP;
prefix[to]+=-numP;
}
// method 1
// for(int i=1;i<prefix.length;i++){
// prefix[i]=prefix[i]+prefix[i-1];
// }
// for(int i=0;i<prefix.length;i++){
// if(prefix[i]>capacity){
// return false;
// }
// }
//method 2
for (int i = 0; capacity >= 0 && i < 1001; ++i) capacity -= prefix[i];
return capacity>=0;
}
M2) O(NLogN) S(N)
public boolean carPooling(int[][] trips, int capacity) {
Map<Integer, Integer> m = new TreeMap<>();
for (int[] t : trips) {
m.put(t[1], m.getOrDefault(t[1], 0) + t[0]);
m.put(t[2], m.getOrDefault(t[2], 0) - t[0]);
}
for (int v : m.values()) {
capacity -= v;
if (capacity < 0) {
return false;
}
}
return true;
}
---------------------------------------------------------->
17. Check if a binary tree is subtree of another binary tree
-> Asked in (Amazon)
-> So there are two methods ->(first one is in Binary tree file)
#) Optimised O(N) S(N) Method
-> Firstly we collect inorder and preorder traversals of both the trees in an arraylist.
-> We know that inorder and preorder uniquely defines a binary tree that's why we are using this.
-> Now we just check that inorder of substree is a subarray of big tree and same with preorder.
-> This can be checked using "KMP" for optimised solution.
-> It may happen that even after having both the subarray still we can say that small tree is not a subtree of Big.
( Tree1
x
/ \
a b
/
c
Tree2
x
/ \
a b
/ \
c d
)
-> So to avoid this condition we just return any character say '$' when node becomes null while collecting inorder or preorder.
-> So to become a subtree both of its inorder and preorder should be subarrays of Big tree.
---------------------------------------------------------->
18. Find the maximum sum leaf to root path in a Binary Tree
-> Asked in (Amazon)
Approach is simple O(N), S(N)
-> First Traversal is to find the maximum sum path.
-> Next traversal is to find that path using below code.
public boolean leaf2rootpath(TreeNode node,int sum,ArrayList<TreeNode>list){
if(node==null){
return false;
}
if(node.val==sum && node.left==null && node.right==null){
list.add(node);
return true;
}
boolean lc=node2rootpath(node.left,sum-node.val,list);
if(lc){
list.add(node);
return true;
}
boolean rc=node2rootpath(node.right,sum-node.val,list);
if(rc){
list.add(node);
return true;
}
return false;
}
---------------------------------------------------------->
19. Duplicate Zeros (LC-1089)
-> Asked in (Amazon)
-> Do it in O(N) & S(1)
-> Basically we are just counting zero and that will be the length of the final array but we have to fit it in the
original array so that's why we are doing this
-> We traverse from last of the array and just dry run the code to understand it better.
public void duplicateZeros(int[] arr) {
int countZero = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] == 0) countZero++;
}
int len = arr.length + countZero;
//We just need O(1) space if we scan from back
//i point to the original array, j point to the new location
for (int i = arr.length - 1, j = len - 1; i < j; i--, j--) {
if (arr[i] != 0) {
if (j < arr.length) arr[j] = arr[i];
} else {
if (j < arr.length) arr[j] = arr[i];
j--;
if (j < arr.length) arr[j] = arr[i]; //copy twice when hit '0'
}
}
}
---------------------------------------------------------->
20. Decode a substring recursively endcoded as count followed by substring.
-> Link: https://www.geeksforgeeks.org/decode-string-recursively-encoded-count-followed-substring/
-> Asked in (Amazon)
Time Complexity -> O(N)
input - > abbbababbbababbbab
output - > 3[a3[b]1[ab]]
-> Here in this question, we push if we encounter character, digit or '['.
-> If we get ']' then we will pop until we reach '[' then pop() to remove '['. and also collect append characters.
-> Now Stack peek will be a number either 1 digit or more than 1 digit.
-> just pop() that whole number and say it is count.
-> now append the same string count no of times. {eg ac and count=3, then acacac will be the string)
-> After that push all the characters of that appended string.
-> At last empty the stack and make the string and return it.
static String decodedString(String s){
Stack<Character> st = new Stack<>();
char[] ch = s.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < ch.length; i++) {
char c = ch[i];
if (Character.isDigit(c) || Character.isAlphabetic(c) || c == '[') {
st.push(c);
} else {
while (st.peek() != '[') {
sb.insert(0, st.pop());
}
st.pop();
String cs="";
while(!st.isEmpty() && Character.isDigit(st.peek())){
cs = st.pop()+cs;
}
int count = Integer.parseInt(cs);
count--;
String str = sb.toString();
while (count-- > 0) {
sb.append(str);
}
for (char cc : sb.toString().toCharArray()) {
st.push(cc);
}
sb = new StringBuilder();
}
}
while(!st.isEmpty())sb.append(st.pop());
return sb.reverse().toString();
}
#) See the Recursive Approach also of this problem.
-> https://www.geeksforgeeks.org/decode-a-string-recursively-encoded-as-count-followed-by-substring-set-2-using-recursion/?ref=rp
---------------------------------------------------------->
21. Longest ZigZag Path in a Binary Tree (LC-1372)
-> Asked in (Amazon)
-> Both solutions has O(N) T.C
M1)
public int longestZigZag(TreeNode root) {
return Math.max(goZigZag(root.left,true,0),goZigZag(root.right,false,0));
}
public int goZigZag(TreeNode node,boolean dir, int len){
if(node==null)return len;
int followZZ = 0;
int selfStart = 0;
if(dir){ //if prev direction was left
followZZ = goZigZag(node.right,false,len+1); //continue follow zigzag path
selfStart = goZigZag(node.left,true,0); // start your own path.
}else{ // if prev direction was right
followZZ = goZigZag(node.left,true,len+1); //continue follow zigzag path
selfStart = goZigZag(node.right,false,0); // start your own path.
}
return Math.max(followZZ,selfStart);
}
M2)
int maxStep = 0;
public int longestZigZag(TreeNode root) {
dfs(root, true, 0);
// dfs(root, false, 0); // means if you give only 1 direction then also it will work.
return maxStep;
}
private void dfs(TreeNode root, boolean isLeft, int step) {
if (root == null) return;
maxStep = Math.max(maxStep, step); // update max step sofar
if (isLeft) {
dfs(root.left, false, step + 1); // keep going from root to left
dfs(root.right, true, 1); // restart going from root to right
} else {
dfs(root.right, true, step + 1); // keep going from root to right
dfs(root.left, false, 1); // restart going from root to left
}
}
---------------------------------------------------------->
22. Basic Calculator (LC-224)
-> ASked in (Amazon)
-> O(N)
-> Dry Run the code to undertand it properly.
public int calculate(String s) {
int res=0,sign=1;
Stack<Integer>st = new Stack<>();
int n=s.length();
char[] crr = s.toCharArray();
for(int i=0;i<n;i++){
char ch = crr[i];
if(Character.isDigit(ch)){
int num = ch-'0';
while(i+1<n && Character.isDigit(crr[i+1])){
num = num*10 + crr[i+1]-'0';
i++;
}
res += num * sign;
}else if(ch=='+'){
sign=1;
}else if(ch=='-'){
sign=-1;
}else if(ch=='('){
st.push(res);
st.push(sign);
res=0; // to get the new number inside the parenthesis.
sign=1;
}else if(ch==')'){
res = res * st.pop() + st.pop();
}
}
return res;
}
---------------------------------------------------------->
23. Longest Common Prefix
-> Asked in (Amazon)
public String longestCommonPrefix(String[] strs) {
if (strs == null || strs.length == 0)
return "";
Arrays.sort(strs); //lexicographically
String first = strs[0];
String last = strs[strs.length - 1];
int c = 0;
while(c < first.length())
{
if (first.charAt(c) == last.charAt(c))
c++;
else
break;
}
return c == 0 ? "" : first.substring(0, c);
}
---------------------------------------------------------->
24. Longest Palindromic Substring
public String longestPalindrome(String s) {
int start = 0;
int end = 0;
for (int i = 0; i < s.length(); i++) {
//StringBuffer sb = new StringBuffer();
//sb.append(s.charAt[i]);
char c = s.charAt(i);
int left = i;
int right = i;
// go to extreme boundaries in case of duplicate characters in left and right.
while (left >= 0 && s.charAt(left) == c) {
left--;
}
while (right < s.length() && s.charAt(right) == c) {
right++;
}
while (left >= 0 && right < s.length()) {
if (s.charAt(left) != s.charAt(right)) {
break;
}
left--;
right++;
}
// left + 1 and right - 1 are actually the start and end index of the Palindromic string
// we don't set "right" because String.substring function required end index exclusively
left = left + 1;
if (end - start < right - left) {
start = left;
end = right;
}
}
return s.substring(start, end);
}
---------------------------------------------------------->
25) Next Greater Element 2 (LC-503)
-> Asked in (Amazon)
-> Basically array is circular here so we just traverse 2n length.
public int[] nextGreaterElements(int[] nums) {
int n = nums.length;
int[]res = new int[n];
Arrays.fill(res,-1);
Stack<Integer>st = new Stack<>();
for(int i=0;i<2*n;i++){
while(!st.isEmpty() && nums[i%n]>nums[st.peek()]){
res[st.pop()]=nums[i%n];
}
st.push(i%n);
}
return res;
}
#) Next Greater Element 3 (LC-556)
-> Exact same as (Next Permutation problem)
---------------------------------------------------------->
26) Find K-th Smallest Pair Distance (LC-719)
-> Asked in ()
-> Here, we are applying binary search on ans (which is our absolute diff of pairs).
-> For every mid we are checking how many pairs can be possible less than equal to mid.
-> To check the above condition i.e no of pairs we just used sliding window technique.
public int smallestDistancePair(int[] nums, int k) {
int n = nums.length;
Arrays.sort(nums);
int lo = 0;
int hi=nums[n-1];
while(lo<hi){
int mid = (lo+hi)/2;
if(checkAbsDiffCount(nums,mid)<k){
lo=mid+1;
}else{
hi=mid; // because the elements of array are not distinct. (dry run to understand this case)
}
}
return lo;
}
public int checkAbsDiffCount(int[]nums,int diff){
int count=0;
int i=0,j=0;
while(j<nums.length){
int d = nums[j]-nums[i];
while(i<nums.length && nums[j]-nums[i]>diff){
i++;
}
count+=j-i;
j++;
}
return count;
}
---------------------------------------------------------->
27) Nuts And Bolts Problem
-> Asked in (Amazon)
public void MatchPairs(char[]nuts,char[]bolts,int low,int hi){
if(low<hi){
int pivot=partition(nuts,low,hi,bolts[hi]);
partition(bolts,low,hi,nuts[pivot]);
MatchPairs(nuts,bolts,low,pivot-1);
MatchPairs(nuts,bolts,pivot+1,hi);
}
}
public int partition(char[]arr,int l,int h,char ch){
int i=l;
int j=l;
while(j<h){
if(arr[j]<ch){
swap(arr,i,j);
i++;
}else if(arr[j]==ch){
swap(arr,j,h);
j--;
}
j++;
}
swap(arr,i,h);
return i;
}
---------------------------------------------------------->
28) Maximum Number of Weeks for which you can work (LC-1953)
-> Asked in (Amazon)
public long numberOfWeeks(int[] milestones) {
long sum = 0;
long max = -1;
for(int stones:milestones){
sum+=stones;
max = Math.max(max,stones);
}
//if sum of all the milestones of projects except maximum one + 1 <= milestones in max project
// it means that all the milestones can be covered.
// but if not than max project milestones - sum(except max one)-1 can be done.
long x = sum-max;
if(x+1>=max)return sum;
else return 2*x+1;
}
---------------------------------------------------------->
29) Given a binary tree and a value k. A path is called heavy path if the sum of the elements in the path (path from root to leaf) > k remove all the paths from the tree which are not heavy i.e., tree should contain only heavy paths.
public TreeNode heavyPath(TreeNode node,int ssf,int limit){
if(node==null)return null;