Reservoir Sampling:One Pass When You Do Not Know n Up Front
Keep the first k items, then for each later index i (1-based), replace a slot with probability k/i — a uniform sample of k from a stream of unknown length.
Read MoreBrowse the full archive. Use topics in the sidebar to explore by tag.
Keep the first k items, then for each later index i (1-based), replace a slot with probability k/i — a uniform sample of k from a stream of unknown length.
Read MoreSquare the base and consume the exponent bit by bit — O(log exp) multiplies instead of a loop of exp products, for integers or modular pow.
Read MoreFor i from n-1 down to 1, swap a[i] with a uniform index in 0..i — every permutation equally likely, which is what Collections.shuffle already runs.
Read MoreReplace repeated subtraction with remainder, then back-substitute for Bézout coefficients — gcd, lcm, and a modular inverse when gcd is 1.
Read Mored × w counters and the min over hashes: over-estimate frequencies on purpose when a HashMap of every key will not fit.
Read MoreA decision guide for the Java Collections series: given the hot operation, null policy, encounter order, and whether another thread looks — which JDK type to new.
Read MoreBuild a trie of all needles, add failure links like KMP on a forest, then scan the haystack once — every pattern that ends at the current character reports.
Read MoreEstimate cardinality with a handful of registers and a harmonic mean — plus-or-minus is the point, not a HashSet of every key.
Read MoreLearn how Java 21 sequenced collections provide uniform first, last, and reversed operations across lists, deques, ordered sets, and ordered maps.
Read MoreAlign the pattern at the window end, use a bad-character skip (and the good-suffix idea) so a late mismatch jumps the haystack instead of sliding by one.
Read More