Skip to content

Instantly share code, notes, and snippets.

View GreggSetzer's full-sized avatar

Gregg GreggSetzer

  • USA
View GitHub Profile
@GreggSetzer
GreggSetzer / queue-stack.js
Created June 22, 2018 19:01
JavaScript Interview Question: Create a Queue using Stacks
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Create a Queue implementation using two stacks. Do not use an
* array inside the Queue class. Queue should implement add() and
* remove().
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
class Stack {
constructor() {
this.data = [];
}
@GreggSetzer
GreggSetzer / queue.js
Last active June 22, 2018 15:45
JavaScript Interview Question: Queue
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Solve some arbitrary problem using a queue.
* The interviewer has asked to provide a solution to a problem
* using a Queue as the data structure. Using the native Array
* in JavaScript with restricted access to its full list of methods
* is one possible solution.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
//Queues implement the FIFO method. First in, first out.
class Queue {
@GreggSetzer
GreggSetzer / fibonacci.js
Created June 22, 2018 14:40
JavaScript Interview Question: Fibonacci
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Write a program to find a fibonacci number.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Iterative solution
// O(n) Linear Runtime Complexity
// This is an alternative approach to solving fibonacci
// without recursion. However, this shoudn't be the
// final answer in an interview setting.
@GreggSetzer
GreggSetzer / refactoring-for-loops-example-2.js
Last active June 20, 2018 15:22
Simplifying JavaScript code using Functional Programming
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
* Given an array of orders, find the top 3 customers with the
* highest lifetime value (those that spent the most over time).
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
// * * * * * * * * * * * * * * * * * * * * * * * * * * *
// Start with the initial implementation.
//
// One approach to solving this task would be to create
@GreggSetzer
GreggSetzer / revealing-module-pattern.js
Created March 2, 2018 17:19
Javascript Interview Question: Revealing Module Pattern
/*
Use the module pattern to group together similar methods such as database services.
In the example below, the ItemDao (Data Access Object) simulates several functions
that interact with a database.
1. Use modules to encapsulate related functionality.
2. A module pattern at its core is an object literal.
3. When using the module in code, call the functions using the key/value syntax.
For example: itemDao.findOne(1);
4. Wrap the object into a function to create a closure for private variables. Examples
@GreggSetzer
GreggSetzer / constructor-pattern.js
Last active March 2, 2018 01:00
Javascript Interview Question: Constructor Pattern
/*
1. Use the constructor pattern for object creation using the new keyword.
2. Each instance of the object will have its own reference to this.
3. Link functions to the object using the prototype. This provides a
performance gain because functions aren't copied into each
instance of the object. They are linked to the prototype.
It's better to have one copy of functions around, not multiple copies.
Now, if we can use ES 2015 syntax:
1. Use the class keyword.
@GreggSetzer
GreggSetzer / capitalize.js
Last active February 3, 2018 20:35
Javascript Interview Question: Title Case - Capitalize the first letter in each word of a string.
/*
Create a function that accepts a string, converts the string to title case, and
returns the result. For title case, the first letter of each word is capitalized.
capitalize('hello world'); //Outputs: 'Hello World'
captialize('the year of the hare'); //Outputs: 'The Year Of The Hare';
Pseudo code:
1. Split the string into array using str.split(' ');
2. Map over the elements in the array. Title case each word using helper function.
@GreggSetzer
GreggSetzer / chunked-array.js
Created January 30, 2018 18:57
Javascript Interview Question: Chunked Array - Given an array, create a new array containing chunked arrays of the given size.
/*
Given an array and size, create a new array containing chunked elements with the size provided.
For example, given the array ['Alligator', 'Bear', 'Cat', 'Dog', 'Elephant', 'Flamingo', 'Giraffe'] and size 2,
return an array of arrays with 2 elements per array.
Result:
[["Alligator", "Black Bear"], ["Cat", "Dog"], ["Elephant", "Flamingo"], ["Giraffe"]]
*/
@GreggSetzer
GreggSetzer / fizz-buzz.js
Created January 30, 2018 18:04
Javascript Interview Question: Fizz Buzz
/*
Create a function that console logs the numbers from 1 to n.
- Print "fizz" for multiples of three instead of the number.
- Print "buzz" for multiples of five instead of the number.
- Print "fizzbuzz" for numbers that are multiples of three and five instead of the number.
- Otherwise, print the number itself.
*/
function fizzBuzz(num) {
for (let i = 1; i <= num; i++) {
@GreggSetzer
GreggSetzer / max-char.js
Last active February 3, 2018 20:35
Javascript Interview Question: Given a string, what character appears the most?
/*
Given a string, find the character that has the highest frequency.
*/
function maxChar(str) {
//Step 1: Create a data store.
let ds = {};
//Step 2: Populate it.
for (let char of str) {