Skip to content

Instantly share code, notes, and snippets.

@mmbarboza
mmbarboza / currying.md
Last active December 3, 2019 16:18 — forked from li-helen/currying.md

Variadic Curry

Prompt

'Curry' is a name for a utility function commonly employed in a functional programming context. Typically, the curry function takes in a function with arity N ('arity' simply means 'the number of arguments a function takes') and returns a function with an arity of 1.

When called, the returned function applies its argument to the original function, and then returns a new function with arity of one. When called, this returned function applies its argument, in turn, returning another function with an arity of one, and so on-- until all of the arguments have been passed in to the original function, at which point it is actually called and the actual result is returned.

We can manually curry funcitons without a currying utility function. For example:

Substring Search

Prompt

Write a function, indexOf, which takes in two strings--the needle and the haystack--and returns the index of the first appearance of the needle in the haystack. If the needle does not appear in the haystack, return -1. Don't use indexOf(), includes() or substring(),

indexOf('or', 'hello world'); // should return 7
indexOf('hello world', 'or'); // should return -1
indexOf('howdy', 'hello world'); // should return -1

Slides


Prompt

Write a function that determines whether an input string has balanced brackets.

You are given an input string consisting of brackets—square [ ], round ( ), and curly { }. The input string can include other text. Write a function that returns either true if the brackets in the input string are balanced or false if they are not. Balanced means that any opening bracket of a particular type must also have a closing bracket of the same type.

@mmbarboza
mmbarboza / anagram_detector_interviewers.md
Created August 16, 2019 01:16
Anagram detector - for interviewers only!

Prompt

You are given an array of strings only (could be words, phrases etc). Create a function to find all the anagrams within that array. The output should be an array where each element in the array is itself an array of anagrams.


Examples

const words = ['cat', 'act', 'ignore', 'a phrase', 'tape', 'pate', 'e hpsara'];
class Node {
constructor(value) {
this.value = value;
this.next = null;
}
}
class LinkedList {
constructor() {
this.head = null;
#Binary Search
Write a function that implements binary search.
Problem
Given a sorted array of numbers, locate the index of a specified value according to the following algorithm.First identify the middle number in the array.Next determine if the value to be found is greater than, lower than, or equal to the middle number.If it is equal, you are finished, and output the index of the found value.If not, repeat the procedure for a smaller array, formed from by taking half of the given array that the specified number falls into.
THIS IS AN EXAMPLE FOR THE INTERVIEWER. HAVE YOUR CANDIDATE COME UP WITH THEIR OWN EXAMPLE!
Example
Consider the array[1, 3, 4, 7, 12, 17, 20].We want to locate the index of 17. First compare 17 to the middle of the array, which is 7. Because 17 > 7 we then repeat the comparison using the subarray[12, 17, 20].We find that 17 matches the middle of this array, and so we output the index from the original array, which is 5. Note that we do not output the index of 17 from the smaller suba