Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 1.15
- // okay so here you're given POSITIONS which is array
- // of indices. And what can we do with indices?
- // yep, we can use them as indices to our original
- // string
- // this looks tricky at first sight
- function scrambleString(s, positions) {
- var result = "";
- for (var i = 0; i < positions.length; i++) {
- // note the common pattern again
- // however, trick is to know what you want to add
- // to result
- // from description: "in the order specified by the indices of the array of indices"
- result += s[positions[i]]; // positions[i] contains an index you want to use for your scrambling
- }
- return result;
- }
- // 1.16
- // just remember the definition of the
- // prime -- the only divisors are 1 and n
- // meaning that if a number is divisible by some other number
- // from 2 to n-1 then it's not a prime
- // also not a prime if it's less than 1
- // now let's put it together
- //
- // note the pattern here
- // function returns true
- // but if one of the conditions in the body is satisfied
- // then it returns false
- // useful to checking stuff like primes
- function isPrime(n) {
- // less than 1 -- not a prime
- if (n < 1) {
- return false;
- }
- for (var i = 2; i < n; i++) { // check numbers from 2 to n-1
- if (n%i === 0) { // if N is divisible by a number from 2 to n-1
- return false; // N is not a prime
- }
- }
- // passed all checks -> N is a prime!
- return true;
- }
- // 1.17
- // how can we find nth prime? well we use previous helper
- // function and count how many primes we get until
- // we get nth prime
- function nthPrime(n) {
- var num_primes = 0; // how many primes we found so far
- var i = 2;
- // note that we use while (true) because we don't know
- // when we're gonna stop, we just keep looking for primes
- // we do know that it will stop because there is an Nth prime
- // number. The general rule is when you know how many things
- // you wanna look through (e.g. number of elements in the array or i = 2..n-1)
- // you use a for loop. When you don't know and you're just looking for stuff
- // you use while (true) infinite loop.
- while (true) {
- if (isPrime(i)) { // is this a prime number
- num_primes++; // we found one more prime
- if (num_primes == n) { // is this the Nth prime number
- return i; // return it
- }
- }
- }
- }
- // 1.18
- // trick is to look for every possible substring that could
- // be palindrome and then store the max length one (this is your max trick pattern)
- // the useful thing to remember is to how to look through every substring of a string
- // e.g. for "iamken"
- // substrings are "i" "ia" "iam" "iamk" "iamken"
- // "a" "am" "amk" "amke" "amken"
- // "m" "mk" "mke" ... "mken"
- // ... ...
- // ... "n"
- // look it's a nested loop, I would just memorize this pattern
- // I put it here but it's not part of solution obviously
- function substrings(s) {
- for (var i = 0; i < s.length; i++) { // each character in the original string
- for (var j = i + 1; j < s.length + 1; j++) { // gives rise to a bunch of substrings
- // e.g. "iamken"
- // look at character "m"
- // substrings starting from character "m" are "m" "mk" "mke" "mken"
- // this is what the double loop is doing precisely
- // iterating through substrings that start with a given character
- var s0 = s.slice(i, j);
- console.log('substring: ', s0);
- }
- }
- }
- // this makes problem way more manageable
- function longestPalindrome(s) {
- var maxLen = Number.MIN_VALUE; // max trick
- var maxPalindrome = null;
- // use substring trick
- for (var i = 0; i < s.length; i++) {
- for (var j = i + 1; j < s.length + 1; j++) {
- var s0 = s.slice(i, j);
- if (isPalindrome(s0)) { // check if palindrome
- // now use max trick to find new max
- if (s0.length > maxLen) { // do
- maxLen = s0.length;
- maxPalindrome = s0;
- }
- }
- }
- }
- return maxPalindrome;
- }
- // helper function since we're looking for a palindrome
- function isPalindrome(s) {
- // you already know this trick
- return s.split("").reverse().join("") === s;
- }
- // 1.19
- // check every single number starting from min(a, b) up until 1
- // you'll find your greatest common factor there
- function gcf(a, b) {
- for (var i = Math.min(a, b); i >= 1; i--) { // check all numbers from min(a, b) to 1
- if (a%i == 0 && b%i == 0) { // is this a greatest common factor
- return i;
- }
- }
- };
- // just for fun, if you have time
- // you can memorize this one liner for gcd based on long division
- // and shove it into your interviewer's face
- function gcd(a, b) {
- if (b == 0) { return a; }
- return gcd(b, a % b);
- }
- // 1.20
- // solution is garbage, here's a better solution
- // instead of dealing with char codes and shit
- // you can say let's make an array containing our
- // alphabet and use that for shifting
- function caesarCipher(offset, s) {
- var result = "";
- var alphabet = "abcdefghijklmnopqrstuvwxyz";
- // iterate over string trick
- for (var i = 0; i < s.length; i++) {
- // we don't shift spaces, we leave them as is
- if (s[i] === " ") {
- result += " ";
- continue;
- }
- // take index of the letter in our alphabet
- var letterIndex = alphabet.indexOf(s[i]);
- // shift it
- // modulo is used for standard trick to ensure value
- // is between 0 and alphabet.length
- // look at this
- // alphabet.length = 26
- // if offset = 8
- // and letterIndex is 20
- // letterIndex + offset = 28 > 26 which is bad
- // but we want to wrap around so 28 % 26 = 2 -> awesome!
- var shiftedIndex = (letterIndex + offset) % alphabet.length;
- // take new letter from the alphabet at shiftedIndex
- result += alphabet[shiftedIndex];
- }
- return result;
- }
- // 1.21
- // okay here's a problem which sounds stupid
- // and you have to ask your interviewer questions
- // to understand what they mean
- // they want number of letters that repeat more than once
- // meaning that in string "abbac" there are 2 letters
- // that repeat more than once ("a" and "b") so the return value is 2
- // this makes it easier to write
- // check each letter, if it repeats more than once add it to counter
- // BUT need to keep track of letters we already counted so we don't
- // double count
- function numRepeats(s) {
- // keep track of letters already counted
- var alreadyCounted = [];
- var result = 0;
- for (var i = 0; i < s.length; i++) {
- // only do work if we haven't checked this letter already
- // remember this is a trick to check if s[i] is NOT in the alreadyCounted array
- if (alreadyCounted.indexOf(s[i]) === -1) {
- // if letter repeats
- if (countLetter(s, s[i]) > 1) {
- result++;
- }
- // we counted this letter, now update alreadyCounted array
- alreadyCounted.push(s[i]);
- }
- }
- return result;
- }
- // helper function to count number of times letter appears in the string
- function countLetter(s, letter) {
- var result = 0;
- for (var i = 0; i < s.length; i++) {
- if (s[i] === letter) {
- result++;
- }
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement