Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
// Problem 1: fizzbuzzChallenge
// write a function that accepts a number and
// 1. returns a string "fizz" if it's divisible by 3,
// 2. returns string "buzz" if divisible by 5,
// 3. returns "fizzbuzz" of divisible by both 3 and 5
// 4. Otherwise returns the input number (if above conditions are not matched)
const fizzbuzzChallenge = num => {
if (typeof num !== "number") {
return null;
}
if (num % 3 === 0 && num % 5 === 0) {
return "fizzbuzz";
} else if (num % 3 === 0) {
return "fizz";
} else if (num % 5 === 0) {
return "buzz";
} else {
return num;
}
};
//Time-complexity: Constant O(1)
//Test fizzbuzzChallenge
console.log(fizzbuzzChallenge(9) === "fizz");
console.log(fizzbuzzChallenge(10) === "buzz");
console.log(fizzbuzzChallenge(15) === "fizzbuzz");
console.log(fizzbuzzChallenge(7) === 7);
console.log(fizzbuzzChallenge(0) === "fizzbuzz");
console.log(fizzbuzzChallenge() === null);
console.log(fizzbuzzChallenge("string") === null);
// Problem 2: reverseChallenge
//write a function that accepts an array and returns another array containing values in reverse order
const reverseChallenge = array => {
const newArray = [];
const m = array.length / 2;
const n = array.length;
for (let i = 0; i < m; i++) {
newArray[i] = array[n - i - 1];
newArray[n - i - 1] = array[i];
}
return newArray;
};
//Time-complexity: O(n-1)
//Test reverseChallenge
const isArrayEqual = (array1, array2) => {
if (array1.length !== array2.length) {
return false;
}
for (let j = 0; j < array1.length; j++) {
if (array1[j] !== array2[j]) {
return false;
} else {
return true;
}
}
};
const isEqual = (a, b) => {
if (Array.isArray(a) && Array.isArray(b)) {
return isArrayEqual(a, b);
} else {
return a === b;
}
};
console.log(isEqual(reverseChallenge([1, 2, 3]), [3, 2, 1]));
console.log(isEqual(reverseChallenge([1]), [1]));
console.log(isEqual(reverseChallenge([0]), [0]));
console.log(isEqual(reverseChallenge(["a", "b", "c"]), ["c", "b", "a"]));
console.log(
isEqual(reverseChallenge(["apple", "banana", "cherry"]), [
"cherry",
"banana",
"apple"
])
);
console.log(isEqual(reverseChallenge(["apple"]), ["apple"]));
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Oct 14, 2019

Problem 3: Write a function which accepts an array as input and returns array of duplicates else empty array.
ex: input: [1,2,2], output: [2]
Problem 4: Write a function which accepts an array and returns true if it is a pallindrome else returns false.
ex: input: [1,2,1], output: true

@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Oct 21, 2019

// Test
// Objective: 
// 1. Program is working for main-conditions
// 2. Program is working for boundary-conditions

// How
// Compare expected-output with actual-output
// isPallindrome([1,2,1]) === true
// isPallindrome([1,1]) === true
// isPallindrome([1,1,3]) === false
@ShilpiMaurya

This comment has been minimized.

Copy link
Owner Author

ShilpiMaurya commented Oct 24, 2019

//Problem 4: Write a function which accepts an array and returns true if it is a pallindrome else returns false.ex: input: [1,2,1], output: true

const isPalindrome = array => {
  const l = array.length;
  const m = Math.floor(l / 2);
  for (let i = 0; i < m; i++) {
    if (array[i] !== array[l - 1 - i]) {
      return false;
    }
  }
  return true;
};
//Time Complexity:O(n/2)

//Test

console.log(isPalindrome([1, 2, 1]) === true);
console.log(isPalindrome([1, 2, 2, 1]) === true);
console.log(isPalindrome([1, 2, 3]) === false);
console.log(isPalindrome(["a", "b", "a"]) === true);
console.log(isPalindrome(["a", "b", "c"]) === false);
console.log(isPalindrome([]) === true);
console.log(isPalindrome([1]) === true);
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Oct 26, 2019

  • map, forEach, Object.keys, Object.values, Object.entries
  • type conversion in js
  • algo online course in js
@ShilpiMaurya

This comment has been minimized.

Copy link
Owner Author

ShilpiMaurya commented Nov 5, 2019

// Problem 3: Write a function which accepts an array as input and returns array of duplicates else empty array.
// ex: input: [1,2,2], output: [2]

const getDuplicates = input => {
  if (!Array.isArray(input) || !input.length) {
    return [];
  }
  const count = {};
  const returnArray = [];

  input.forEach(item => {
    if (count[item]) {
      returnArray.push(item);
    } else {
      count[item] = 1;
    }
  });
  return returnArray;
};

const isArrayEqual = (array1, array2) => {
  if (
    !Array.isArray(array1) ||
    !Array.isArray(array2) ||
    array1.length !== array2.length
  ) {
    return false;
  }
  for (let j = 0; j < array1.length; j++) {
    if (array1[j] !== array2[j]) {
      return false;
    }
  }
  return true;
};

//Time Complexity:O(n)

//tests

console.log(isArrayEqual(getDuplicates([1, 2, 3, 2, 4, 5]), [2]));
console.log(isArrayEqual(getDuplicates([1, 2, 3, 4, 5]), []));
console.log(isArrayEqual(getDuplicates(), []));
console.log(isArrayEqual(getDuplicates(["a", "b", "c", "d", "a"]), ["a"]));
console.log(isArrayEqual(getDuplicates([]), []));
console.log(isArrayEqual(getDuplicates(1), []));
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Nov 6, 2019

// array1 === array2
// 1 === 1 // compared by value, since they are stored on call-stack
// {} === {} // compared by reference, as they are stored on memory-heap

@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Nov 6, 2019

image

@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Nov 6, 2019

Problem 3.1:

Write a function which accepts an array as input and returns true if it has no-duplicates otherwise false.

Problem 5:

Write a function which accepts a string as input, the string would contain ' characters, returns true if all the quotes have been closed otherwise false.

test1: input: "asfdsfs'sdfds'sdfsd", output: true
test2: input: "asfdsfs'sdf'ds'sdfsd", output: false

@ShilpiMaurya

This comment has been minimized.

Copy link
Owner Author

ShilpiMaurya commented Nov 15, 2019

// Write a function which accepts an array as input and returns true
// if it has no-duplicates otherwise false.

const hasDuplicates = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  const count = {};
  for (let i = 0; i < arr.length; i++) {
    const key = arr[i];
    if (count[key]) {
      return true;
    } else {
      count[key] = 1;
    }
  }
  return false;
};

//Time-Complexity: O(n)
//tests

console.assert(hasDuplicates([1, 2, 2, 3, 4]) === true, "test 1");
console.assert(hasDuplicates([1, 2, 2, 3, 3, 4]) === true, "test 2");
console.assert(hasDuplicates([1, 2, 3, 4]) === false, "test 3");
console.assert(hasDuplicates([]) === null, "test 4");
console.assert(hasDuplicates(1, 2, 3) === null, "test 5");

// Write a function which accepts a string as input,
// the string would contain ' characters, returns true
// if all the quotes have been closed otherwise false.
// test1: input: "asfdsfs'sdfds'sdfsd", output: true
// test2: input: "asfdsfs'sdf'ds'sdfsd", output: false

const isAllQuotesClosed = input => {
  if (typeof input !== "string") {
    return null;
  }
  let count = 0;
  for (let i = 0; i < input.length; i++) {
    const str = input[i];
    if (str === "'") {
      count = count + 1;
    }
  }
  if (count % 2 === 0) {
    return true;
  } else {
    return false;
  }
};

//Time-Complexity: O(n)
//tests

console.assert(isAllQuotesClosed("asfdsfs'sdfds'sdfsd") === true, "test 6");
console.assert(isAllQuotesClosed("asfdsfs'sdf'ds'sdfsd") === false, "test 7");
console.assert(isAllQuotesClosed(1234) === null, "test 8");
console.assert(isAllQuotesClosed() === null, "test 9");
@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Nov 16, 2019

Programming Challenge 6

Write a function

function solution(A);
that, given an array A consisting of N integers, returns the number of distinct values in array A.

For example, given array A consisting of six elements such that:

A[0] = 2 A[1] = 1 A[2] = 1
A[3] = 2 A[4] = 3 A[5] = 1
the function should return 3, because there are 3 distinct values appearing in array A, namely 1, 2 and 3.

Write an efficient algorithm for the following assumptions:

N is an integer within the range [0..100,000];
each element of array A is an integer within the range [−1,000,000..1,000,000].

@ShilpiMaurya

This comment has been minimized.

Copy link
Owner Author

ShilpiMaurya commented Nov 17, 2019

// Write a function

// function solution(A);
// that, given an array A consisting of N integers,
// returns the number of distinct values in array A.

// For example, given array A consisting of six elements such that:

// A[0] = 2 A[1] = 1 A[2] = 1
// A[3] = 2 A[4] = 3 A[5] = 1
// the function should return 3, because there are 3 distinct values
// appearing in array A, namely 1, 2 and 3.

// Write an efficient algorithm for the following assumptions:

// N is an integer within the range [0..100,000];
// each element of array A is an integer within the range
// [−1,000,000..1,000,000].

const distinctValues = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  const count = {};
  const result = [];
  arr.forEach(item => {
    if (count[item]) {
      result.push(item);
    } else {
      count[item] = "s";
    }
  });
  return arr.length - result.length;
};

//Time-complexity: O(n)
// tests

console.assert(distinctValues([1, 2, 3, 4, 56, 76, 53, 53, 76]) === 7);
console.assert(distinctValues([]) === null);
console.assert(distinctValues(1, 2, 3) === null);

// Other method

const distinctValues = arr => {
  if (!Array.isArray(arr) || !arr.length) {
    return null;
  }
  let count = 0;
  let obj = {};
  arr.forEach(item => {
    if (!obj[item]) {
      obj[item] = 1;
      count = count + 1;
    }
  });
  return count;
};

//Time-complexity:O(n)
//tests

console.assert(distinctValues([1, 2, 3, 4, 56, 76, 53, 53, 76]) === 7);
console.assert(distinctValues([]) === null);
console.assert(distinctValues(1, 2, 3) === null);

@vaibhavKumarTokyo

This comment has been minimized.

Copy link

vaibhavKumarTokyo commented Nov 17, 2019

Please refresh your js string manipulation here (or someplace else):
https://developer.mozilla.org/en-US/docs/Learn/JavaScript/First_steps/Useful_string_methods

Next challenge:

Programming challenge 7.1

Create a function called fixStart. It should take a single argument, a string, and return a version where all occurences of its first character have been replaced with '*', except for the first character itself. You can assume that the string is at least one character long. For example:

fixStart('babble'): 'ba**le'

Programming challenge 7.2

Create a function called notBad that takes a single argument, a string.

  • It should find the first appearance of the substring 'not' and 'bad'.
  • If the 'bad' follows the 'not', then it should replace the whole 'not'...'bad' substring with 'good' and return the result.
  • If it doesn't find 'not' and 'bad' in the right sequence (or at all), just return the original sentence.
    For example:
  notBad('This dinner is not that bad!'): 'This dinner is good!'
  notBad('This movie is not so bad!'): 'This movie is good!'
  notBad('This dinner is bad!'): 'This dinner is bad!'

Programming challenge 7.3

How to find the maximum occurring character in given String?
If two or more characters have same occurrence then return the first appeared-character in the string.
For example:

solution("Today is Monday"): 'a'

Programming challenge 7.4

How do you count a number of vowels and consonants in a given string?

Programming challenge 7.5

How do you check if a given String contains valid parentheses?
For example:

isValid("this is (properly closed)"): true
isValid("this is) not ((properly closed)"): false
isValid("(())"): true
isValid(")(())"): false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.