Code Explainers

Code explainers tagged #algorithms

ruby
require 'monitor'
 
class TokenBucket
  include MonitorMixin

A thread-safe token bucket rate limiter in Ruby

rate-limiting concurrency thread-safety
Intermediate 8 steps
typescript
export class PriorityQueue<T> {
  private heap: Array<{ value: T; priority: number }> = [];
 
  get size(): number {

Building a binary-heap priority queue in TypeScript

binary-heap priority-queue generics
Intermediate 9 steps
php
final class TokenBucketLimiter
{
    private float $tokens;
    private float $lastRefill;

How a token bucket rate limiter works

rate-limiting token-bucket throttling
Intermediate 7 steps
rust
use std::collections::BinaryHeap;
use std::cmp::Reverse;
 
pub fn top_k<T: Ord + Clone>(items: &[T], k: usize) -> Vec<T> {

Top-K selection with a bounded min-heap in Rust

heap top-k generics
Intermediate 8 steps
python
import time
import threading
 
 

How a thread-safe token bucket rate limiter works

rate-limiting concurrency locking
Intermediate 6 steps
python
from collections import deque
 
 
def bfs(graph, start):

Breadth-first search and shortest paths in Python

graph-traversal bfs queue
Intermediate 9 steps
java
import java.util.*;
 
public class TopologicalSort {
 

Topological sort with Kahn's algorithm

graphs topological-sort bfs
Intermediate 6 steps
java
import java.util.Arrays;
 
public class SlidingWindow {
    // Returns the maximum sum of any contiguous subarray of length k.

The sliding window technique in Java

sliding-window arrays algorithms
Intermediate 8 steps
ruby
# Binary search using Ruby's built-in Array#bsearch.
#
# There are two modes:
#   1. find-minimum mode  -> block returns true/false (monotonic)

Binary search patterns with Ruby's bsearch

binary-search algorithms ranges
Intermediate 7 steps