49
49
- [ Merge Sort In Place Algorithm] ( #merge-sort-in-place-algorithm )
50
50
- [ Find Median Values (With Merge Sort Algorithm)] ( #find-median-values-with-merge-sort-algorithm )
51
51
- [ Quick Sort] ( #quick-sort )
52
+ - [ Why Quick sort is used in Array and Merge Sort in Linked List?] ( #why-quick-sort-is-used-in-array-and-merge-sort-in-linked-list )
52
53
- [ Mathematics & Stats You should know] ( #mathematics--stats-you-should-know )
53
54
- [ How to initialize array of size n?] ( #how-to-initialize-array-of-size-n )
54
55
- [ How many zeros in 1 Billion] ( #how-many-zeros-in-1-billion )
73
74
- [ Binary Search on sorted Array Algorithm] ( #binary-search-on-sorted-array-algorithm )
74
75
- [ Reverse Linked List Algorithm] ( #reverse-linked-list-algorithm )
75
76
- [ Merge Sort Algorithm] ( #merge-sort-algorithm )
77
+ - [ Quick Sort Algorithm] ( #quick-sort-algorithm )
76
78
- [ Breadth-First Binary Tree Traversal] ( #breadth-first-binary-tree-traversal )
77
79
- [ Depth-First Binary Tree Traversal] ( #depth-first-binary-tree-traversal )
78
80
- [ Insert in MIN-HEAP] ( #insert-in-min-heap )
106
108
- [ Reverse linked list] ( #reverse-linked-list )
107
109
- [ Find the merge point of 2 Linked List] ( #find-the-merge-point-of-2-linked-list )
108
110
- [ Binary Search for competitive programming from zero to advanced] ( #binary-search-for-competitive-programming-from-zero-to-advanced )
111
+ - [ Binary Search Concept] ( #binary-search-concept )
112
+ - [ Binary Search Basic Problems] ( #binary-search-basic-problems )
113
+ - [ Binary Search Advanced Questions] ( #binary-search-advanced-questions )
109
114
- [ References] ( #references )
110
115
111
116
> Coding interview question answers in JavaScript for Facebook, Amazon, Google, Microsoft or any company.
@@ -580,7 +585,7 @@ Merge Sort Implementation Visualization:
580
585
581
586
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" yLMQvbv " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
582
587
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/yLMQvbv " >
583
- Merge Sort In-place Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
588
+ Merge Sort In-place Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
584
589
on <a href =" https://codepen.io " >CodePen</a >.</span >
585
590
</p >
586
591
@@ -635,6 +640,10 @@ When Browser's are not using Merge sort they most of the time use Quick sort var
635
640
- [ quick sort in-place] ( src/algorithms/sorting/quick-sort/quick-sort-inplace.mjs )
636
641
- [ quick sort ] ( src/algorithms/sorting/quick-sort/quick-sort.mjs )
637
642
643
+ ### Why Quick sort is used in Array and Merge Sort in Linked List?
644
+
645
+ In Quick sort we do not create auxiliary arrays. Therefore, it is good choice for Array to use quick sort. However in merge sort we create 2 auxiliary arrays. Therefore, linked list is a good choice.
646
+
638
647
## Mathematics & Stats You should know
639
648
640
649
### How to initialize array of size n?
@@ -643,7 +652,7 @@ Example create an array containing numbers from 0 to 9.
643
652
` [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] `
644
653
645
654
``` js
646
- var y = new Array.from (Array (10 ).keys ())
655
+ var y = new Array.from (Array (10 ).keys ());
647
656
```
648
657
649
658
![ ] ( https://i.imgur.com/mVBT7jl.png )
@@ -829,7 +838,7 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
829
838
830
839
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" XWRrPBE " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
831
840
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/XWRrPBE " >
832
- Binary Search on Sorted Array Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
841
+ Binary Search on Sorted Array Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
833
842
on <a href =" https://codepen.io " >CodePen</a >.</span >
834
843
</p >
835
844
@@ -839,7 +848,7 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
839
848
840
849
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js " data-slug-hash =" WNjegBp " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
841
850
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/WNjegBp " >
842
- Merge Sort Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
851
+ Merge Sort Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
843
852
on <a href =" https://codepen.io " >CodePen</a >.</span >
844
853
</p >
845
854
@@ -849,7 +858,17 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
849
858
850
859
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js " data-slug-hash =" WNjegBp " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
851
860
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/WNjegBp " >
852
- Merge Sort Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
861
+ Merge Sort Algorithm</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
862
+ on <a href =" https://codepen.io " >CodePen</a >.</span >
863
+ </p >
864
+
865
+ ### Quick Sort Algorithm
866
+
867
+ ![ ] ( https://i.imgur.com/LAouqTY.png )
868
+
869
+ <p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" OJpBYKz " data-user =" rupeshtiwari " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
870
+ <span >See the Pen <a href =" https://codepen.io/rupeshtiwari/pen/OJpBYKz " >
871
+ Quick Sort In-place Implementation: Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/rupeshtiwari " >@rupeshtiwari </a >)
853
872
on <a href =" https://codepen.io " >CodePen</a >.</span >
854
873
</p >
855
874
@@ -859,15 +878,15 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
859
878
860
879
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" XWRroaz " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
861
880
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/XWRroaz " >
862
- Binary Tree Traversal</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
881
+ Binary Tree Traversal</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
863
882
on <a href =" https://codepen.io " >CodePen</a >.</span >
864
883
</p >
865
884
866
885
### Depth-First Binary Tree Traversal
867
886
868
887
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" ExmYGze " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
869
888
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/ExmYGze " >
870
- </a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
889
+ </a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
871
890
on <a href =" https://codepen.io " >CodePen</a >.</span >
872
891
</p >
873
892
@@ -879,7 +898,7 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
879
898
880
899
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" PomwXJX " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
881
900
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/PomwXJX " >
882
- Practice</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
901
+ Practice</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
883
902
on <a href =" https://codepen.io " >CodePen</a >.</span >
884
903
</p >
885
904
@@ -889,7 +908,7 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
889
908
890
909
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" PomwXJX " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
891
910
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/PomwXJX " >
892
- Practice</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
911
+ Practice</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
893
912
on <a href =" https://codepen.io " >CodePen</a >.</span >
894
913
</p >
895
914
@@ -901,8 +920,8 @@ All Mandatory algorithm source code [download here](https://codepen.io/collectio
901
920
902
921
** O(Log(n)) time | Space O(1)**
903
922
904
- For 1 insert operation, avg case is O(lgn ) and worst case is O(n)
905
- For n insert operations, avg case is O(nlgn ) and worst case is O(n^2)
923
+ For 1 insert operation, avg case is O(log(n) ) and worst case is O(n)
924
+ For n insert operations, avg case is O(n log(n) ) and worst case is O(n^2)
906
925
907
926
![ ] ( https://i.imgur.com/8fWHF2B.png )
908
927
@@ -926,7 +945,7 @@ For n insert operations, avg case is O(nlgn) and worst case is O(n^2)
926
945
927
946
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" rNmBqWQ " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
928
947
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/rNmBqWQ " >
929
- Binary Search Tree Implementation</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
948
+ Binary Search Tree Implementation</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
930
949
on <a href =" https://codepen.io " >CodePen</a >.</span >
931
950
</p >
932
951
@@ -945,15 +964,15 @@ Download solutions to [Facebook Recruiting Portal Coding](https://www.facebookre
945
964
946
965
<p class =" codepen " data-height =" 265 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" MWpxezz " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Graph: DFS Question (easy) " >
947
966
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/MWpxezz " >
948
- Graph: DFS Question (easy)</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
967
+ Graph: DFS Question (easy)</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
949
968
on <a href =" https://codepen.io " >CodePen</a >.</span >
950
969
</p >
951
970
952
971
#### Depth First Search Answer
953
972
954
973
<p class =" codepen " data-height =" 560 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" NWpJrjg " style =" height : 560px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Graph: DFS Answer (easy) " >
955
974
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/NWpJrjg " >
956
- Graph: DFS Answer (easy)</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
975
+ Graph: DFS Answer (easy)</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
957
976
on <a href =" https://codepen.io " >CodePen</a >.</span >
958
977
</p >
959
978
@@ -1030,15 +1049,15 @@ Delete 15 from BST
1030
1049
1031
1050
<p class =" codepen " data-height =" 265 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" wvJRxJp " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Delete node in BST Question " >
1032
1051
<span>See the Pen <a href="https://codepen.io/roopkt/pen/wvJRxJp">
1033
- Delete node in BST Question</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@roopkt </a>)
1052
+ Delete node in BST Question</a> by Rupesh Tiwari (<a href="https://codepen.io/roopkt">@rupeshtiwari </a>)
1034
1053
on <a href="https://codepen.io">CodePen</a>.</span>
1035
1054
</p >
1036
1055
1037
1056
[ Answer] ( https://codepen.io/roopkt/pen/bGqOxyN )
1038
1057
1039
1058
<p class =" codepen " data-height =" 265 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" rNyovyL " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Delete node in BST Answer " >
1040
1059
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/rNyovyL " >
1041
- Delete node in BST Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
1060
+ Delete node in BST Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
1042
1061
on <a href =" https://codepen.io " >CodePen</a >.</span >
1043
1062
</p >
1044
1063
<script async src =" https://cpwebassets.codepen.io/assets/embed/ei.js " ></script >
@@ -1067,15 +1086,15 @@ Example: Find in-order successor of `6`?
1067
1086
1068
1087
<p class =" codepen " data-height =" 265 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" YzZdMxa " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Find In-order Successor in BST Question " >
1069
1088
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/YzZdMxa " >
1070
- Find In-order Successor in BST Question</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
1089
+ Find In-order Successor in BST Question</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
1071
1090
on <a href =" https://codepen.io " >CodePen</a >.</span >
1072
1091
</p >
1073
1092
1074
1093
[ Answer] ( https://codepen.io/roopkt/pen/bGqOxyN )
1075
1094
1076
1095
<p class =" codepen " data-height =" 327 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" bGqOxyN " style =" height : 327px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Find In-order Successor in BST Answer " >
1077
1096
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/bGqOxyN " >
1078
- Find In-order Successor in BST Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
1097
+ Find In-order Successor in BST Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
1079
1098
on <a href =" https://codepen.io " >CodePen</a >.</span >
1080
1099
</p >
1081
1100
@@ -1160,7 +1179,7 @@ Try `Binary Search`.
1160
1179
1161
1180
<p class =" codepen " data-height =" 300 " data-theme-id =" dark " data-default-tab =" js,result " data-slug-hash =" dyvwPej " data-user =" roopkt " style =" height : 300px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" >
1162
1181
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/dyvwPej " >
1163
- Reverse Linked List: Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
1182
+ Reverse Linked List: Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
1164
1183
on <a href =" https://codepen.io " >CodePen</a >.</span >
1165
1184
</p >
1166
1185
@@ -1175,27 +1194,43 @@ Try `Binary Search`.
1175
1194
1176
1195
<p class =" codepen " data-height =" 265 " data-theme-id =" dark " data-default-tab =" js,result " data-user =" roopkt " data-slug-hash =" poeqBBb " style =" height : 265px ; box-sizing : border-box ; display : flex ; align-items : center ; justify-content : center ; border : 2px solid ; margin : 1em 0 ; padding : 1em ;" data-pen-title =" Find merge point of 2 linked list Answer " >
1177
1196
<span >See the Pen <a href =" https://codepen.io/roopkt/pen/poeqBBb " >
1178
- Find merge point of 2 linked list Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@roopkt </a >)
1197
+ Find merge point of 2 linked list Answer</a > by Rupesh Tiwari (<a href =" https://codepen.io/roopkt " >@rupeshtiwari </a >)
1179
1198
on <a href =" https://codepen.io " >CodePen</a >.</span >
1180
1199
</p >
1181
1200
1182
1201
## Binary Search for competitive programming from zero to advanced
1183
1202
1203
+ Binary Search is used to solve 2 kinds of problems:
1204
+ 1- Search problems, find real numbers.
1205
+ 2- Optimization problems. Maximize element x keeping y minimum. Or minimize element x keeping y maximum.
1206
+
1207
+ ### Binary Search Concept
1208
+
1184
1209
- Watch [ Code School Binary Search Videos] ( https://www.youtube.com/watch?v=j5uXyPJ0Pew&list=PL2_aWCzGMAwL3ldWlrii6YeLszojgH77j )
1185
- - Watch this [ Binary Search video and do problems] ( https://www.youtube.com/watch?v=GU7DpgHINWQ&list=PLl0KD3g-oDOHpWRyyGBUJ9jmul0lUOD80 )
1186
- - Do all basic problems related to binary search [ that I have given in this link] ( https://codepen.io/collection/vBORpO ) .
1210
+ - Watch this [ Binary Search conceptual video from Errichto and do problems] ( https://www.youtube.com/watch?v=GU7DpgHINWQ&list=PLl0KD3g-oDOHpWRyyGBUJ9jmul0lUOD80 )
1187
1211
1212
+ ### Binary Search Basic Problems
1188
1213
1214
+ - Do all basics problems related to binary search [ that I have given in this link] ( https://codepen.io/collection/vBORpO )
1215
+ - Next do all below advanced problems
1189
1216
1217
+ ### Binary Search Advanced Questions
1190
1218
1219
+ Below problems are lying under optimization problems. Just watch Book Allocation and Aggressive Cows videos to understand optimization logic. Once you understood them after that for rest problems you need to apply same logic.
1220
+
1221
+ - [ Book Allocation] ( https://www.youtube.com/watch?v=Ss9ta1zmiZo&t=1335s )
1222
+ - [ Aggressive Cows] ( https://www.youtube.com/watch?v=TC6snf6KPdE )
1223
+ - ROTI (SPOJ)
1224
+ - Painter's Partition
1225
+ - EKO (SPOJ)
1226
+ - PARATA (SPOJ)
1191
1227
1192
1228
## References
1193
1229
1194
1230
- http://btholt.github.io/four-semesters-of-cs/
1195
1231
- https://btholt.github.io/four-semesters-of-cs-part-two/
1196
1232
- [ Binary Tree Visualization] ( https://www.cs.usfca.edu/~galles/visualization/BST.html )
1197
- - [[ Part 3] Binary Search, Competitive Programming Series | from Zero to Advanced] ( https://www.youtube.com/watch?v=cLkcNeiploY&list=PL4PCksYQGLJOcaPLgeMFaxaHigPFjBuTG&index=4 )
1198
-
1233
+ - [[ Part 3] Binary Search, Competitive Programming Series | from Zero to Advanced] ( https://www.youtube.com/watch?v=cLkcNeiploY&list=PL4PCksYQGLJOcaPLgeMFaxaHigPFjBuTG&index=4 )
1199
1234
1200
1235
<script async src =" https://cpwebassets.codepen.io/assets/embed/ei.js " ></script >
1201
1236
<script data-ad-client =" ca-pub-1700383344966810 " async =" " src =" https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js " data-checked-head =" true " ></script >
0 commit comments