Skip to content

Instantly share code, notes, and snippets.

@kirkdrichardson
kirkdrichardson / longest_unique_substring.js
Created November 18, 2019 04:28
JavaScript - Find the Longest Substring of Unique Characters in O(N) - Sliding Window Pattern
/**
* Write a function called longestSubstring, which accepts a string and returns the
* length of the longest substring with all distinct characters.
*
* O(N)
*
*/
function longestSubstring(str) {
let longest = 0;
@kirkdrichardson
kirkdrichardson / has_duplicates.js
Created November 18, 2019 04:25
Use a Frequency Counter Pattern to Check for Duplicates in Javascript Array
/**
* Implement a function called hasDuplicates which accepts
* a variable number of integer arguments and returns
* true if there are duplicates in the arguments.
*
* Should be solved in O(n) or better.
*
*/
function hasDuplicates(...args) {
@kirkdrichardson
kirkdrichardson / has_duplicates.js
Created November 18, 2019 04:25
Use a Frequency Counter Pattern to Check for Duplicates in Javascript Array
/**
* Implement a function called hasDuplicates which accepts
* a variable number of integer arguments and returns
* true if there are duplicates in the arguments.
*
* Should be solved in O(n) or better.
*
*/
function hasDuplicates(...args) {
@kirkdrichardson
kirkdrichardson / main.dart
Created October 14, 2019 03:30
Sliding Window Pattern - algorithm challenge
/**
* 🔥🔥🔥🔥 Sliding Window Pattern 🔥🔥🔥🔥
*
* - Easy to implement
* - Can be tricky to reason about
* - Good at making O(n^2) Subset / Subarray problems O(n)
*
* -----
* | sum |
* -----
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 14, 2019 03:28
Multiple Pointer Pattern (slinky variation) - algorithm challenge
/**
*
* Two Pointers Technique:
* - Simple to implement
* - Typically improves time complexity from O(n^2) to O(n).
* - Often used for searching for pairs in a sorted array
*
* Write a fx named uniqueValues that accepts a sorted array, and
* returns an integer representing the number of unique values in the array.
* The array may contain negative values.
@kirkdrichardson
kirkdrichardson / main.dart
Last active October 14, 2019 03:29
Multiple Pointer Pattern (closing walls variation) - algorithm challenge
/**
* Multiple Pointer Pattern Challenge 👻 closing in on the middle 👻
*
* see live at https://dartpad.dartlang.org/1df653a9a71a17e78e58397c040033ca
*
* Write a fx called sumZero which accepts a sorted array of integers.
* The fx should find the first pair where the sum is 0.
*
*
@kirkdrichardson
kirkdrichardson / main.dart
Created October 12, 2019 23:37
Callable classes in Dart
// Callable classes in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/ab72121a4c0fe4d1a3c29697d9a108dd
void main() {
MyCallableClass()();
}
class MyCallableClass {
@kirkdrichardson
kirkdrichardson / main.dart
Created October 12, 2019 23:23
Basic Data Structures in Dart
// Basic Data Structures in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/a4dd3a27e5750a029de689391ae90004
void main() {
print('------------------FIXED LENGTH LIST------------------------');
List<int> l = List(3);
print(l.length);
@kirkdrichardson
kirkdrichardson / main.dart
Created October 12, 2019 02:49
Frequency Counter Pattern in Dart - anagram challenge
/**
*
* Frequency Counter Pattern in Dart - anagram challenge
*
* Problem Description:
*
* Given two strings, write a function to determine if the second string is an anagram
* of the first. Do not worry about whitespace or non-alphanumeric characters.
*
@kirkdrichardson
kirkdrichardson / main.dart
Created October 12, 2019 02:19
Basic Functional Programming in Dart
// Basic Functional Programming in Dart (as of v2.5)
// See live at https://dartpad.dartlang.org/8608f4b406d6fbd4504f9a328aa7b044
void main() {
// Functions are objects
print('-----------------ANONYMOUS (LAMBDA) FUNCTIONS--------------------');