Skip to content

Instantly share code, notes, and snippets.

@hdf
Last active December 22, 2018 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hdf/7fcb1032e308b1e1c5c433fdc4a22164 to your computer and use it in GitHub Desktop.
Save hdf/7fcb1032e308b1e1c5c433fdc4a22164 to your computer and use it in GitHub Desktop.
Interview stuff?
// Functional one-liner solution to FizzBuzz from: https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
Array.from(Array(100).keys()).map(v=>v+1).forEach(i=>console.log(''+(i%3?'':'Fizz')+(i%5?'':'Buzz')||i))
# My Python3 solution to https://imranontech.com/2007/01/24/using-fizzbuzz-to-find-developers-who-grok-coding/
for i in range(1, 101):
tmp = '' if i % 3 else 'Fizz'
tmp += '' if i % 5 else 'Buzz'
print (tmp or i)
# One-liner version:
print('\n'.join('Fizz'*(not i%3)+'Buzz'*(not i%5) or str(i) for i in range(1,101)))
// Inspired by:
// https://interviewing.io/recordings/Go-Microsoft-1
function NthClosest(points, match, n) {
if (n < 1 || n > points.length || points[0].length != match.length)
return false;
function distance(p1, p2) { // Works in any dimensions.
let out = 0, d = 0, len = p1.length;
for (let i3 = 0; i3 < len; i3++) {
d = p1[i3] - p2[i3];
out += d * d; // No need to square root, as we don't care about the actual value, only the order.
}
return out;
}
function place(heap, dist) { // Before which one should we put this one?
let len = heap.length;
for (let i2 = 0; i2 < len; i2++)
if (heap[i2][0] > dist)
return i2;
return len;
}
let heap = [], len = points.length;
for (let i = 0; i < len ; i++) { // Creating a sorted heap. Not paying attention to duplicate values though.
let dist = distance(points[i], match);
heap.splice(place(heap, dist), 0, [dist, i]);
if (heap.length > n)
heap.pop();
}
//console.log(heap);
return points[heap[n - 1][1]];
}
console.log(NthClosest([[1,2],[3,-1],[2,1],[2,3]], [2,2], 2));
// Inspired by:
// https://www.youtube.com/watch?v=D35llNtkCps
function NthSmallest(arr, n) {
var smallests = [];
function Smallest(arr, ignore) {
var out = 0;
while (ignore !== false && out < arr.length-1 && arr[out] <= ignore) // Don't start with something that is invalid
out++;
for (let i = out + 1; i < arr.length; i++)
if (arr[i] < arr[out] && (ignore === false || arr[i] > ignore))
out = i;
return out;
}
for (let i = 0; i < n; i++) {
let last = smallests.length - 1;
let tmp = arr[Smallest(arr, (i > 0)?smallests[last]:false)];
if (i > 0 && tmp < smallests[last]) // If n is larger than the number of unique elements
break;
smallests.push(tmp);
}
//console.log(smallests);
return smallests[smallests.length-1];
}
console.log(NthSmallest([5,19,5,8,4,9,1,0], 4));
function NthSmallest2(arr, n) {
for (let i = 0; i < n; i++) {
for (let i2 = arr.length - 1; i2 > i; i2--) {
if (arr[i2] < arr[i2-1]) {
let tmp = arr[i2-1];
arr[i2-1] = arr[i2];
arr[i2] = tmp;
}
}
}
//console.log(arr);
return arr[n-1];
}
console.log(NthSmallest2([5,19,5,8,4,9,1,0], 4));
// Inspired by:
// https://interviewing.io/recordings/Java-LinkedIn-1/
var arr = ['p', 'e', 'r', 'f', 'e', 'c', 't', ' ',
'm', 'a', 'k', 'e', 's', ' ',
'p', 'r', 'a', 'c', 't', 'i', 'c', 'e'];
function ReverseSegment(arr, begin, end) {
begin = (typeof begin == "undefined")?0:begin;
end = (typeof end == "undefined")?arr.length - 1:end;
while (begin < end) {
let tmp = arr[begin];
arr[begin] = arr[end];
arr[end] = tmp;
begin++;
end--;
}
}
function ReverseWords(arr) {
function IndexesOf(arr, elem) {
var out = [];
for (let i = 0; i < arr.length; i++)
if (arr[i] == elem) out.push(i);
return out;
}
ReverseSegment(arr);
let spaces = [-1].concat(IndexesOf(arr, ' '));
let l = spaces.push(arr.length) - 1;
for (let i = 0; i < l; i++)
ReverseSegment(arr, spaces[i] + 1, spaces[i + 1] - 1);
}
console.log(arr.join(''));
ReverseWords(arr);
console.log(arr.join(''));
// Inspired by:
// https://interviewing.io/recordings/C++-Airbnb-2/
function UniqueArrayMissingOne(arr1, arr2) {
var smaller, bigger;
if (arr1.length < arr2.length) {
bigger = arr2;
smaller = arr1;
} else {
bigger = arr1;
smaller = arr2;
}
if (bigger.length - smaller.length != 1) return false;
for (let i = 0; i < bigger.length; i++) {
let found = false;
for (let i2 = 0; i2 < smaller.length; i2++) {
if (bigger[i] == smaller[i2]) {
found = true;
break;
}
}
if (found === false)
return bigger[i];
}
}
console.log(UniqueArrayMissingOne([1,9,5,4,3,2], [3,1,5,9,2]));
function UniqueArrayDiff(arr1, arr2) {
let common = [];
let filtered = arr1.filter(e => {
let loc = arr2.indexOf(e);
if (loc < 0) return true;
common.push(arr2[loc]);
return false;
});
return filtered.concat(arr2.filter(e => !common.includes(e)));
}
console.log(UniqueArrayDiff([1,9,5,4,3,0,2], [3,1,8,5,9,2]));
function UniqueArrayDiff2(arr1, arr2) { // Turns out to be much faster!
return arr1.filter(e => {
let loc = arr2.indexOf(e);
if (loc < 0) return true;
arr2.splice(loc, 1);
return false;
}).concat(arr2);
}
console.log(UniqueArrayDiff2([1,9,5,4,3,0,2], [3,1,8,5,9,2]));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment