|
66 | 66 | - [Get the Average value at each level of the tree](#get-the-average-value-at-each-level-of-the-tree)
|
67 | 67 | - [ADT](#adt)
|
68 | 68 | - [Time Memory Trade-Off technique](#time-memory-trade-off-technique)
|
| 69 | +- [Mandatory Algorithms](#mandatory-algorithms) |
| 70 | + - [Binary Search Algorithm](#binary-search-algorithm) |
| 71 | + - [Reverse Linked List Algorithm](#reverse-linked-list-algorithm) |
| 72 | + - [Merge Sort Algorithm](#merge-sort-algorithm) |
69 | 73 | - [Coding Interview Question and Answers](#coding-interview-question-and-answers)
|
70 | 74 | - [Graphs](#graphs)
|
71 | 75 | - [Depth First Search Question](#depth-first-search-question)
|
@@ -772,6 +776,53 @@ abstract data type (ADT) - ADT is defined as a user point of view of a data type
|
772 | 776 |
|
773 | 777 | Trade off or invest some memory to improve run time complexity. Suppose use Has-table, set etc. to insert some of the calculations that you want to not repeat.
|
774 | 778 |
|
| 779 | + |
| 780 | + |
| 781 | +## Mandatory Algorithms |
| 782 | + |
| 783 | +### Binary Search Algorithm |
| 784 | + |
| 785 | + |
| 786 | +<div class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="XWRrPBE" data-user="roopkt" data-prefill='{"title":"Binary Search Algorithm","tags":[],"scripts":[],"stylesheets":[]}'> |
| 787 | + <pre data-lang="js">// O(log (n)) time | O(1) space |
| 788 | +function binarySearch(array, valueToSearch) { |
| 789 | + let low =0; let high = array.length-1; |
| 790 | + while(low<=high) { |
| 791 | + let mid = low + Math.floor((high-low)/2); |
| 792 | + if(valueToSearch === array[mid]) return mid; // Found value, return (exit) |
| 793 | + else if (valueToSearch < array[mid]) high = mid-1; // Search in left |
| 794 | + else low = mid + 1; // Search in right |
| 795 | + } |
| 796 | + return -1; |
| 797 | +}</pre></div> |
| 798 | + |
| 799 | +### Reverse Linked List Algorithm |
| 800 | + |
| 801 | + |
| 802 | +<div class="codepen" data-height="300" data-theme-id="dark" data-default-tab="js" data-slug-hash="jOmNvRw" data-user="roopkt" data-prefill='{"title":"Reverse Linked List","tags":[],"scripts":[],"stylesheets":[]}'> |
| 803 | + <pre data-lang="js">// O(n) time | O(1) space |
| 804 | +function reverse(head) { |
| 805 | + let current = head; |
| 806 | + let previous = null; |
| 807 | + let next = null; |
| 808 | + |
| 809 | + while (current) { |
| 810 | + next = current.next; |
| 811 | + current.next = previous; |
| 812 | + previous = current; |
| 813 | + current = next; |
| 814 | + } |
| 815 | + |
| 816 | + head = previous; |
| 817 | + |
| 818 | + return head; |
| 819 | +}</pre></div> |
| 820 | + |
| 821 | +### Merge Sort Algorithm |
| 822 | + |
| 823 | + |
| 824 | + |
| 825 | + |
775 | 826 | ## Coding Interview Question and Answers
|
776 | 827 |
|
777 | 828 | ### Graphs
|
@@ -1003,6 +1054,7 @@ Try `Binary Search`.
|
1003 | 1054 | </p>
|
1004 | 1055 |
|
1005 | 1056 | - [Question](https://codepen.io/roopkt/pen/mdWayxv?editors=0010)
|
| 1057 | +- |
1006 | 1058 | - [Answer](https://codepen.io/roopkt/pen/dyvwPej?editors=0011)
|
1007 | 1059 |
|
1008 | 1060 | #### Find the merge point of 2 Linked List
|
|
0 commit comments