Advertisement
Guest User

Untitled

a guest
Jul 15th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // 1.15
  2. // okay so here you're given POSITIONS which is array
  3. // of indices. And what can we do with indices?
  4. // yep, we can use them as indices to our original
  5. // string
  6. // this looks tricky at first sight
  7. function scrambleString(s, positions) {
  8.   var result = "";
  9.   for (var i = 0; i < positions.length; i++) {
  10.     // note the common pattern again
  11.     // however, trick is to know what you want to add
  12.     // to result
  13.     // from description: "in the order specified by the indices of the array of indices"
  14.     result += s[positions[i]]; // positions[i] contains an index you want to use for your scrambling
  15.   }
  16.   return result;
  17. }
  18.  
  19. // 1.16
  20. // just remember the definition of the
  21. // prime -- the only divisors are 1 and n
  22. // meaning that if a number is divisible by some other number
  23. // from 2 to n-1 then it's not a prime
  24. // also not a prime if it's less than 1
  25. // now let's put it together
  26. //
  27. // note the pattern here
  28. // function returns true
  29. // but if one of the conditions in the body is satisfied
  30. // then it returns false
  31. // useful to checking stuff like primes
  32. function isPrime(n) {
  33.   // less than 1 -- not a prime
  34.   if (n < 1) {
  35.     return false;
  36.   }
  37.   for (var i = 2; i < n; i++) { // check numbers from 2 to n-1
  38.     if (n%i === 0) { // if N is divisible by a number from 2 to n-1
  39.       return false; // N is not a prime
  40.     }
  41.   }
  42.   // passed all checks -> N is a prime!
  43.   return true;
  44. }
  45.  
  46. // 1.17
  47. // how can we find nth prime? well we use previous helper
  48. // function and count how many primes we get until
  49. // we get nth prime
  50. function nthPrime(n) {
  51.   var num_primes = 0; // how many primes we found so far
  52.   var i = 2;
  53.   // note that we use while (true) because we don't know
  54.   // when we're gonna stop, we just keep looking for primes
  55.   // we do know that it will stop because there is an Nth prime
  56.   // number. The general rule is when you know how many things
  57.   // you wanna look through (e.g. number of elements in the array or i = 2..n-1)
  58.   // you use a for loop. When you don't know and you're just looking for stuff
  59.   // you use while (true) infinite loop.
  60.   while (true) {
  61.     if (isPrime(i)) { // is this a prime number
  62.       num_primes++; // we found one more prime
  63.       if (num_primes == n) { // is this the Nth prime number
  64.         return i; // return it
  65.       }
  66.     }
  67.   }
  68. }
  69.  
  70. // 1.18
  71. // trick is to look for every possible substring that could
  72. // be palindrome and then store the max length one (this is your max trick pattern)
  73. // the useful thing to remember is to how to look through every substring of a string
  74. // e.g. for "iamken"
  75. // substrings are "i" "ia" "iam" "iamk" "iamken"
  76. //                     "a" "am" "amk" "amke" "amken"
  77. //                     "m"  "mk" "mke" ...    "mken"
  78. //                      ... ...
  79. //                      ...                   "n"
  80. // look it's a nested loop, I would just memorize this pattern
  81. // I put it here but it's not part of solution obviously
  82. function substrings(s) {
  83.   for (var i = 0; i < s.length; i++) { // each character in the original string
  84.     for (var j = i + 1; j < s.length + 1; j++) { // gives rise to a bunch of substrings
  85.       // e.g. "iamken"
  86.       // look at character "m"
  87.       // substrings starting from character "m" are "m" "mk" "mke" "mken"
  88.       // this is what the double loop is doing precisely
  89.       // iterating through substrings that start with a given character
  90.       var s0 = s.slice(i, j);
  91.       console.log('substring: ', s0);
  92.     }
  93.   }
  94. }
  95.  
  96. // this makes problem way more manageable
  97. function longestPalindrome(s) {
  98.   var maxLen = Number.MIN_VALUE; // max trick
  99.   var maxPalindrome = null;
  100.   // use substring trick
  101.   for (var i = 0; i < s.length; i++) {
  102.     for (var j = i + 1; j < s.length + 1; j++) {
  103.       var s0 = s.slice(i, j);
  104.       if (isPalindrome(s0)) { // check if palindrome
  105.         // now use max trick to find new max
  106.         if (s0.length > maxLen) { // do
  107.           maxLen = s0.length;
  108.           maxPalindrome = s0;
  109.         }
  110.       }
  111.     }
  112.   }
  113.   return maxPalindrome;
  114. }
  115.  
  116. // helper function since we're looking for a palindrome
  117. function isPalindrome(s) {
  118.   // you already know this trick
  119.   return s.split("").reverse().join("") === s;
  120. }
  121.  
  122. // 1.19
  123. // check every single number starting from min(a, b) up until 1
  124. // you'll find your greatest common factor there
  125. function gcf(a, b) {
  126.   for (var i = Math.min(a, b); i >= 1; i--) { // check all numbers from min(a, b) to 1
  127.     if (a%i == 0 && b%i == 0) { // is this a greatest common factor
  128.       return i;
  129.     }
  130.   }
  131. };
  132.  
  133. // just for fun, if you have time
  134. // you can memorize this one liner for gcd based on long division
  135. // and shove it into your interviewer's face
  136. function gcd(a, b) {
  137.   if (b == 0) { return a; }
  138.   return gcd(b, a % b);
  139. }
  140.  
  141. // 1.20
  142. // solution is garbage, here's a better solution
  143. // instead of dealing with char codes and shit
  144. // you can say let's make an array containing our
  145. // alphabet and use that for shifting
  146. function caesarCipher(offset, s) {
  147.   var result = "";
  148.   var alphabet = "abcdefghijklmnopqrstuvwxyz";
  149.   // iterate over string trick
  150.   for (var i = 0; i < s.length; i++) {
  151.     // we don't shift spaces, we leave them as is
  152.     if (s[i] === " ") {
  153.       result += " ";
  154.       continue;
  155.     }
  156.     // take index of the letter in our alphabet
  157.     var letterIndex = alphabet.indexOf(s[i]);
  158.     // shift it
  159.     // modulo is used for standard trick to ensure value
  160.     // is between 0 and alphabet.length
  161.     // look at this
  162.     // alphabet.length = 26
  163.     // if offset = 8
  164.     // and letterIndex is 20
  165.     // letterIndex + offset = 28 > 26 which is bad
  166.     // but we want to wrap around so 28 % 26 = 2 -> awesome!
  167.     var shiftedIndex = (letterIndex + offset) % alphabet.length;
  168.     // take new letter from the alphabet at shiftedIndex
  169.     result += alphabet[shiftedIndex];
  170.   }
  171.   return result;
  172. }
  173.  
  174. // 1.21
  175. // okay here's a problem which sounds stupid
  176. // and you have to ask your interviewer questions
  177. // to understand what they mean
  178. // they want number of letters that repeat more than once
  179. // meaning that in string "abbac" there are 2 letters
  180. // that repeat more than once ("a" and "b") so the return value is 2
  181. // this makes it easier to write
  182. // check each letter, if it repeats more than once add it to counter
  183. // BUT need to keep track of letters we already counted so we don't
  184. // double count
  185. function numRepeats(s) {
  186.   // keep track of letters already counted
  187.   var alreadyCounted = [];
  188.   var result = 0;
  189.   for (var i = 0; i < s.length; i++) {
  190.     // only do work if we haven't checked this letter already
  191.     // remember this is a trick to check if s[i] is NOT in the alreadyCounted array
  192.     if (alreadyCounted.indexOf(s[i]) === -1) {
  193.       // if letter repeats
  194.       if (countLetter(s, s[i]) > 1) {
  195.         result++;
  196.       }
  197.       // we counted this letter, now update alreadyCounted array
  198.       alreadyCounted.push(s[i]);
  199.     }
  200.   }
  201.   return result;
  202. }
  203.  
  204. // helper function to count number of times letter appears in the string
  205. function countLetter(s, letter) {
  206.   var result = 0;
  207.   for (var i = 0; i < s.length; i++) {
  208.     if (s[i] === letter) {
  209.       result++;
  210.     }
  211.   }
  212.   return result;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement