Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am dzoltok on github.
  • I am davidzoltok (https://keybase.io/davidzoltok) on keybase.
  • I have a public key ASB1FHFC4Y_RdUQUFzpW_mIGFpSfKa7fzpfGzhDqvotX8Qo

To claim this, I am signing this object:

@dzoltok
dzoltok / encoder.js
Created May 11, 2016 22:35
Write an encoder that obfuscates an array in some manner, then write a decoder than can re-produce the original array
// input is an array of strings
// e.g. ['foo', 'bar', '3']
function encode(input) {
var output = "";
input.forEach(function(element) {
output += element.length + '.' + element;
});
return output;
}
@dzoltok
dzoltok / sumPossible.js
Created May 11, 2016 22:34
Returns true if the given target is the sum of two elements in the given array, and false otherwise
function sumPossible(arr, target) {
// Option 1, the naive and slow way, O(n^2)
for (var i = 0; i < arr.length; i ++) {
for (var j = i +1; j < arr.length; j ++) {
if (arr[i] + arr[j] === target) {
return true;
}
}
}
@dzoltok
dzoltok / mergeLists.js
Created May 11, 2016 22:31
Given two sorted linked lists, write a function to produce a single list containing all elements of both lists, then use it to write a function that does the same for k lists
/**
* Question
*
* Write a function that uses merge(list1, list2) to merge an array of lists into a single list
*
* Analysis
*
* O(nk), where k is the number of lists and n is the maximum length among the lists
*/
function mergeAll(lists) {
@dzoltok
dzoltok / bracketMatcher.js
Created May 11, 2016 22:13
Basic bracket matching problem
/**
* Question
*
* Write an algorithm that returns true if a given input has evenly-matched brackets, and false otherwise
*
* Analysis
*
* O(n) where n is the length of the pattern to match
*/
@dzoltok
dzoltok / closestPoints.js
Created May 11, 2016 22:11
Given a set of integer (x,y) coordinates and an integer k, write a function which returns the k points closest to the origin, i.e. (0, 0).
/**
* 'k' points closest to origin
*
* Question
*
* Given a set of integer (x,y) coordinates and an integer k, write a function which returns the k points closest to the origin, i.e. (0, 0).
*
* Assumptions
*
* A coordinate is an object with two properties, 'x' and 'y'