Advertisement
tabvn

Untitled

Jul 23rd, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.61 KB | None | 0 0
  1. # Interview Quiz
  2.  
  3. ## Quick Recap
  4.  
  5. ### Big O
  6.  
  7. Big O is the language and metric we use to describe the efficiency of algorithms. In theory, big O means the upper bound limit, but for practical purposes we'll use the tightest/closer upper bound possible (basically what academics call big theta). For example, iterating over all elements of an array should be described as O(n), although in theory O(n^2), O(n^3) are also valid upper limits.
  8.  
  9. Remember that O(1) < O(log n) < O(n) < O(n * log n) < O(n^2).
  10.  
  11. Also remember to drop constants, basically O(2 * n) could simply be described as O(n).
  12.  
  13. **Time complexity** is the asymptotic runtime of an algorithm. It's how fast it runs. For example, iterating over an array is O(n).
  14.  
  15. **Space complexity** is the asymptotic amount of memory used by an algorithm. It's how much memory it takes.
  16.  
  17. ## Quiz 1 - Array
  18.  
  19. Edit this markdown by replacing the `> EDIT ME`s below with your answers.
  20.  
  21. ### Find key
  22.  
  23. Given an array `A` of alpha characters `[A-Za-z]`, create a function that returns the first index at which a given element can be found in the array, or `-1` if it is not present.
  24.  
  25. **Example of `A`**
  26.  
  27. | i | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 |
  28. | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- | ---- |
  29. | A[i] | G | d | e | Z | e | k | I | X | d | S | e |
  30.  
  31. **Solution**
  32.  
  33.  
  34. ```
  35.  
  36. const findIndex = (array,self, cb) => {
  37. let len = array.length;
  38. let i;
  39. if (len === 0) return -1;
  40. if (typeof cb !== 'function') {
  41. throw new TypeError(cb);
  42. }
  43.  
  44. if (self) {
  45. for (i = 0; i < len; i++) {
  46. if (cb.call(self, array[i], i, array)) {
  47. return i;
  48. }
  49. }
  50. } else {
  51. for (i = 0; i < len; i++) {
  52. if (cb(array[i], i, array)) {
  53. return i;
  54. }
  55. }
  56. }
  57.  
  58. return -1;
  59. }
  60.  
  61.  
  62. let myArray = ["G","d","e","Z","e","k","I","X","d","S","e"];
  63.  
  64. // so now find index of array has value is: "A"
  65. let findValue = "A";
  66. let findItem = findIndex(myArray, findValue, (item) => item === findValue );
  67. console.log(findItem);
  68. // for testing this code you can copy it and paste to : https://babeljs.io/repl/
  69.  
  70. ```
  71.  
  72.  
  73.  
  74. **Time and space complexities**
  75.  
  76. > EDIT ME
  77.  
  78. ### Are values unique?
  79.  
  80. Create another function to identify whether `A` has unique elements.
  81.  
  82. **Solution**
  83.  
  84. ```
  85.  
  86. let myArray = ["G","d","e","Z","e","k","I","X","d","S","e"];
  87. // check if "A" is unique element
  88. let findValue = "A";
  89. let findItem = myArray.filter((i) => i === findValue);
  90. if(findItem.length){
  91. if(findItem.length === 1){
  92. // A is unique
  93. console.log(findValue + ' is unique');
  94. }else{
  95. console.log(findValue + ' is duplicated');
  96. }
  97. }else{
  98. console.log("Not found");
  99. }
  100.  
  101.  
  102. ```
  103.  
  104. **Time and space complexities**
  105.  
  106. > EDIT ME
  107.  
  108. **Extra**: What if you have memory/space limitations and you need to minimize the use of memory/data structures.
  109.  
  110. **Solution**
  111.  
  112. > EDIT ME
  113.  
  114. **Time and space complexities**
  115.  
  116. > EDIT ME
  117.  
  118. ### Find key - extra
  119.  
  120. What if `A` is known to be sorted using unique values, can you improve the performance to find the key?
  121.  
  122. **Example of sorted and unique `A`**
  123.  
  124. ```
  125.  
  126.  
  127. let myArray = ["G","G", "d","e","Z","e","k","I","X","d","S","e"];
  128.  
  129. // let sort the arrays by items are duplicated. more duplicate items will be first of the array.
  130. let sortingArray = myArray.sort((a,b) => {
  131. let l1 = myArray.filter((a1) => a1 == a).length;
  132. let l2 = myArray.filter((b1) => b1 == b).length;
  133. return l2 - l1;
  134. })
  135.  
  136. console.log(sortingArray);
  137.  
  138.  
  139. ```
  140.  
  141. **Solution**
  142.  
  143. > EDIT ME
  144.  
  145. **Time and space complexities**
  146.  
  147. > EDIT ME
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement