Skip to content

Instantly share code, notes, and snippets.

@wilm42
Created July 26, 2017 21:12
Show Gist options
  • Save wilm42/2e758c35ce77b0aaf43756f5f5bcc786 to your computer and use it in GitHub Desktop.
Save wilm42/2e758c35ce77b0aaf43756f5f5bcc786 to your computer and use it in GitHub Desktop.
1: Numbers less than a given number
Imagine you have an array of numbers. Write an algorithm to remove all numbers less than five from the array. You shouldn't use the .filter method here; try to write the algorithm from scratch.
const arr = [1,2,3,4,5,6,7,8,9,10];
const remLessThan = (arr, value) => {
const newArr = [];
arr.forEach(item => {
if(item >= value) {
newArr.push(item);
}
});
return newArr;
}
console.log(remLessThan(arr, 5));
Runtime: Linear // O(n) - We don't think this needs to be optimized further.
2: Merge Arrays
Imagine you have two arrays which have already been sorted. Write an algorithm to merge the two arrays into a single array, which should also be sorted. For example, if your input arrays were [1, 3, 6, 8, 11] and [2, 3, 5, 8, 9, 10], your output array should be [1, 2, 3, 3, 5, 6, 8, 8, 9, 10, 11].
const arr1= [1, 3, 6, 8, 11];
const arr2= [2, 3, 5, 8, 9, 10];
function merge(arr1, arr2){
let d1 = 0;
let d2 = 0;
const returnArr = [];
while(d1 < arr1.length && d2 < arr2.length){
if(arr1[d1] > arr2[d2]){
returnArr.push(arr2[d2++]);
}
else{
returnArr.push(arr1[d1++]);
}
}
while(d1 < arr1.length) {
returnArr.push(arr1[d1++]);
}
while(d2 < arr2.length) {
returnArr.push(arr2[d2++]);
}
console.log(returnArr);
}
merge(arr1,arr2);
Runtime: Linear // O(n) - We don't think this could be optimized further
3: Remove Characters
Write an algorithm that deletes given characters from a string. For example, given a string of "Battle of the Vowels: Hawaii vs. Grozny" and characters to be removed are "aeiou", the algorithm should transform the original string to "Bttl f th Vwls: Hw vs. Grzny".
const str = "Battle of the Vowels: Hawaii vs. Grozny"
// const rem = /[aeiou]/;
const rem = "aeiou";
let tick = 0;
function remover(str,rem){
let returnString = '';
let d1 = 0;
let d2 = 0;
while(d1 < str.length) {
tick++
if(str[d1] !== rem[d2] && d2 === rem.length) {
returnString += str[d1++];
d2 = 0;
}
else if(str[d1] === rem[d2]){
d1++;
d2 =0;
}
else {
d2++;
}
}
console.log(returnString);
}
remover(str,rem);
Runtime: quadratic O(n^2) - Yikes.
4: Products
Given an array of numbers, write an algorithm to find out the products of every number, except the one at that index. For example, if the input was [1, 3, 9, 4], the output should be [108, 36, 12, 27] (i.e. [394, 194, 134, 139]).
const arr = [1, 3, 9, 4, 5, 8];
function products(arr){
let product = 1;
let result = [];
let ticks = 0;
arr.forEach(number => {
ticks++;
product *= number;
});
arr.forEach(number => {
ticks++;
let devided = product/number;
result.push(devided);
});
console.log(result, ticks);
}
products(arr);
Runtime: Linear // O(n) - Even though it loops TWICE over the same array, adding a new item to the array will only increase the number of iterations by 2 - so in direct proportion. Thus, linear.
5: Appointment Book
Write a program that keeps track of your appointment for the day from 8 am - 7 pm for up to 5 days.Your program will allow users to choose a day between Mon-Fri and an hour between 8 am - 7 pm to add the appointment in the appointment book. Use a multi dimentional array to solve this problem. You don't have to have a fancy UI for this, although you are welcome to use React to create a nice GUI and user input and result. Otherwise simple JS with some input boxes and/or checkboxes will do. A very simple example of your output can look like this
const schedule = [[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]],[[],[],[],[],[],[],[],[],[],[],[],[]]]
function input(day,time,input){
schedule[day][time] = input;
}
function output(day){
let time = 8
for(let i=0; i < schedule[day].length; i++){
console.log(`${time}:00 >>>`,schedule[day][i])
time++
}
}
input(0,3,'lol');
output(0);
Runtime: Uhhhh... the first one is O(1), the second is O(n)... can't get much better than that.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment