Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Interview Exercises
// Given a string, write a function to check if it is a permutation of a palindrome.
// A palindrome is a word or phrase that is the same forwards and backwards.
// A permutation is a rearrangement of letters. The palindrome does not need to be
// limited to just dictionary words.
// Expected output:
// racecar -> true
// pcopc -> true (after rearranging letters to pcocp)
// Write a function that takes an array as an argument,
// and returns the string that is the longest common
// prefix of each element in the array.
//
// Example 1: Input: ['abc', 'ab', 'abd']; Output: 'ab'
// Example 2: Input: ['abc', 'acd', 'abd']; Output: 'a'
// Example 3: Input: ['abcdef', 'abcd']; Output: 'abcd'
// Example 4: Input: ['abc', 'cde', 'abd']; Output: '' (empty string)
function longestCommonPrefix(list) {
return "not implemented yet";
}
console.log("Example 1: expected = 'ab'; actual =", longestCommonPrefix(["abc", "ab", "abd"]) );
console.log("Example 2: expected = 'a'; actual =", longestCommonPrefix(["abc", "acd", "abd"]) );
console.log("Example 3: expected = 'abcd'; actual =", longestCommonPrefix(["abcdef", "abcd"]) );
console.log("Example 4: expected = ''; actual =", longestCommonPrefix(["abc", "cde", "abd"]) );
/**
Implement a pyramid function that takes an integer value and prints asterisks up to that number and then back down:
pyramid(3);
*
**
***
**
*
pyramid(5);
*
**
***
****
*****
****
***
**
*
**/
// Remove duplicated items in the string, return a new string.
// Expected result: cat dog lion fish elephant camel bird lizard
let list = "cat dog lion fish elephant camel cat bird dog lizard fish";
// Remove duplicated items in the array two items are considered to be
// duplicates if they have the same keys and each key has the same value.
// Expected result:
// [
// { foo: 'bar' },
// {},
// { bar: 'baz' },
// { foo: { bar: 'baz' } },
// { bar: 'foo', foo: 'baz' }
// ]
list = [
{ foo: "bar" },
{},
{ bar: "baz" },
{ foo: "bar" },
{ foo: { bar: "baz" } },
{ foo: "baz", bar: "foo" },
{ bar: "foo", foo: "baz" }
];
//
// Implement the reverseSpecial function as described below.
//
/**
* Reverse the provided string value while leaving all special characters in their original place.
*
* Some example reversals:
*
* AB#$CD@E%F => FE#$DC@B%A
* #$% => #$%
* AB#C => CB#A
* ABC => CBA
* XA##B => BA##X
*
* @param a string value
* @return the reverse of the string value, with special characters unaffected
*
*/
function reverseSpecial(input) {
return input;
}
function testCase(input, expect) {
const result = reverseSpecial(input);
console.log(result);
if (result === expect) {
console.log('- Passed');
} else {
console.log('- Failed, expected: ' + expect);
}
}
testCase('AB#$CD@E%F', 'FE#$DC@B%A');
testCase('#$%', '#$%');
testCase('AB#C', 'CB#A');
testCase('ABC', 'CBA');
testCase('XA##B', 'BA##X');
testCase('ABCD#$#$', 'DCBA#$#$');
testCase('ab#$cd@e%f', 'fe#$dc@b%a');
testCase('a1#$c3@e%5', '5e#$3c@1%a');
testCase('A1#$c3@E%5f', 'f5#$E3@c%1A');
testCase('ABB#$CDD', 'DDC#$BBA');
// Given an array of integers, return indices of the two numbers such that they add up to a specific target.
// You may assume that each input would have exactly one solution, and you may not use the same element twice.
// Example:
// Given nums = [2, 7, 11, 15], target = 9,
// Because nums[0] + nums[1] = 2 + 7 = 9,
// return [0, 1].
/**
* @param {number[]} nums
* @param {number} target
* @return {number[]}
*/
var twoSum = function(nums, target) {
};
console.log(twoSum([1, 8, 33, 6], 7));
console.log(twoSum([5, 8, 3, 9, 0], 800));
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.