Created
September 16, 2017 06:10
-
-
Save techsin/e8ffa9208fc879e36fb5be48ce9ddb86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Question 1 -- sumOfTwo(a,b,v): You have two integer arrays, a and b, and an integer target value v. Determine whether there is a pair of numbers, where one number is taken from a and the other from b, that can be added together to get a sum of v. Return true if such a pair exists, otherwise return false. | |
Question 2 -- stringReformatting(string): The string s contains dashes that split it into groups of characters. You are given an integer k that represents the number of characters in groups that your output should have. Your goal is to return a new string that breaks s into groups with a length of k by placing dashes at the correct intervals. If necessary, the first group of characters can be shorter than k. It is guaranteed that there are no consecutive dashes in s. | |
For s = "2-4a0r7-4k" and k = 4, the output should be stringReformatting(s, k) = "24a0-r74k"; | |
The input string "2-4a0r7-4k" is split into three groups with lengths of 1, 5 and 2. Since k = 4, you need to split the string into two groups of 4 characters each, making the output string "24a0-r74k". | |
For s = "2-4a0r7-4k" and k = 3, the output should be stringReformatting(s, k) = "24-a0r-74k". | |
Given the same input string and k = 3, split the string into groups of 2, 3, and 3 characters to get the output string of "24-a0r-74k". | |
Question 3 -- getClosingParen(sentence, openingParenIndex): | |
"Sometimes (when I nest them (my parentheticals) too much (like this (and this))) they get confusing." | |
Write a function that, given a sentence like the one above, along with the position of an opening parenthesis, finds the corresponding closing parenthesis. | |
Example: if the example string above is input with the number 10 (position of the first parenthesis), the output should be 79 (position of the last parenthesis). | |
*/ | |
//Q1 | |
//Create hash table for one array then for another array look up for key that is complimentary number to reach target value. | |
//hash map is O(n), and key look up is O(1), so total is O(n) | |
function sumOfTwo(a, b, v) { | |
const h = {}; | |
a.forEach(x => h[x] = 1); | |
return b.some(n => !!h[v-n]); | |
} | |
//Q2 | |
//parse string backwards, ignore -, and using mod operator % figure out if k number of elements have passed | |
function stringReformatting(s, k) { | |
let newStr = ''; | |
let i = s.length; | |
let counter = 1; | |
while (--i > -1) { | |
let char = s[i]; | |
if (char !== '-') { | |
newStr = char + newStr; | |
if (counter % k === 0 && i != 0) { | |
newStr = '-' + newStr; | |
} | |
counter++; | |
} | |
} | |
return newStr; | |
} | |
//Q3 | |
//keep counter, increase when encounter open, decrease when closing parenthisis. When counter = 0, you have found the matching one. | |
//compact version | |
function getClosingParen(str, i) { | |
if (str[i] != '(') return null; | |
let count = 1; | |
while (i < str.length) { | |
let char = str[++i]; | |
count += (char == ')') ? -1 : (char == '(')? 1 : 0; | |
if (count === 0) return i; | |
} | |
return -1; | |
} | |
/* | |
//long version | |
function getClosingParen(str, i) { | |
if (str[i] != '(') return; | |
let stack_count = 1; | |
while (i < str.length) { | |
let char = str[++i]; | |
if (char == '(') { | |
stack_count++; | |
} else if (char == ')') { | |
stack_count--; | |
} | |
if (stack_count === 0) { | |
return i; | |
} | |
} | |
return -1; | |
} | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment