Skip to content

Instantly share code, notes, and snippets.

@hellokvn
hellokvn / polymorphism-bad.ts
Created February 14, 2022 09:59
Polymorphism - Bad
class Dog {
public name: string;
constructor(name: string) {
this.name = name;
}
public makeSound(): void {
process.stdout.write('wuff wuff\n');
}
@hellokvn
hellokvn / polymorphism-good.ts
Created February 14, 2022 09:59
Polymorphism - Good
class Animal {
public name: string;
constructor(name: string) {
this.name = name;
}
public makeSound(): void {
process.stdout.write('generic animal sound\n');
}
@hellokvn
hellokvn / binary-search.ts
Last active February 11, 2024 11:12
Binary Search
function binarySearch(nums: number[], target: number): number {
let left: number = 0;
let right: number = nums.length - 1;
while (left <= right) {
const mid: number = Math.floor((left + right) / 2);
if (nums[mid] === target) return mid;
if (target < nums[mid]) right = mid - 1;
else left = mid + 1;