Skip to content

Instantly share code, notes, and snippets.


David Pennington Xeoncross

View GitHub Profile
Xeoncross / validSudoku.leetcode.go
Created February 18, 2023 17:58
My first attempt at validating a Sukoku board. (Also my first introduction to Sudoku at all!)
View validSudoku.leetcode.go
func isValidSudoku(board [][]byte) bool {
// 9 spaces is > 8 bits so we use 2 bytes (an int16)
rowsMap := [9]int16{}
colsMap := [9]int16{}
gridMap := [9]int16{}
for row:=0; row<9; row++ {
for col:=0; col<9; col++ {
Xeoncross / http_get_request.go
Created December 31, 2022 21:16
Making a HTTP request in Go requires you check multiple things if you want to be safe. Below is an example of something that takes, timeouts, context deadlines, HTTP status codes, and a size limit on the response.
View http_get_request.go
package safehttp
// HttpGetRequest assumes the response fits in memory and you're not trying to
// decode a json payload which would be better using json.NewDecoder()
func HttpGetRequest(ctx context.Context, url string, timeout time.Duration, sizeLimit int64) ([]byte, error) {
// Create a new HTTP client with a timeout
client := http.Client{Timeout: timeout}
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
Xeoncross / mergesort.go
Created December 1, 2022 02:29
Generics merge sort for ordered slices ([]int) inplace (WIP)
View mergesort.go
package mergesort
import (
// based on
Xeoncross /
Created October 6, 2022 14:39
Install Java on mac
# Using homebrew
brew install java
For the system Java wrappers to find this JDK, symlink it with
sudo ln -sfn /usr/local/opt/openjdk/libexec/openjdk.jdk /Library/Java/JavaVirtualMachines/openjdk.jdk
openjdk is keg-only, which means it was not symlinked into /usr/local,
because macOS provides similar software and installing this software in
parallel can cause all kinds of trouble.
Xeoncross / parse_wiki_titles.go
Last active August 18, 2022 18:13
Simple script to parse the lines in the wikipedia page title dump file and output a list of all the english page titles (deduplicated). See for more files
View parse_wiki_titles.go
package wikipediatitles
import (
_ "embed"
Xeoncross / System
Created July 8, 2022 01:22 — forked from vasanthk/System
System Design Cheatsheet
View System

System Design Cheatsheet

Picking the right architecture = Picking the right battles + Managing trade-offs

Basic Steps

  1. Clarify and agree on the scope of the system
  • User cases (description of sequences of events that, taken together, lead to a system doing something useful)
    • Who is going to use it?
    • How are they going to use it?
Xeoncross / ranked_window.go
Created June 22, 2022 23:19
Keep the keys with the highest values in a map
View ranked_window.go
package main
import "fmt"
func BenchmarkMapIteration(b *testing.B) {
size := 1000
ranked := make(map[int]uint8, size)
for i := 1; i < size; i++ {
ranked[i] = uint8(i % 8)
Xeoncross / int_bits.go
Created June 22, 2022 17:36
Examples of bit lengths of different integer values:
View int_bits.go
package main
import (
func main() {
// subtract 1 from each answer since we're counting zero also
fmt.Printf("%d bits = %8b = %d\n", 2, byte(1<<2-1), 1<<2)
Xeoncross / delta_encoding_demo.go
Created June 22, 2022 02:13
Encoding a sorted sequence of integers using delta encoding can save 40%-60% on storage costs:
View delta_encoding_demo.go
package iiexperiments
import (
// How much space will using a delta encoding save?
// If we assume we have 1 byte (0-255) to tell us how many bytes the delta is contained in, we can then only store the diff between the last and next int values. This difference is the "delta".
Xeoncross /
Created June 20, 2022 20:00
Distributed rate limiting with non-whole time periods (max 3 requests every 5 seconds)

Rate Limiting Redis with multiples of a time.Duration

In a distributed system, you want the API layer to be stateless. So the app can offload rate limiting to a redis instance. This way, you can use the INCR to both increment and return the number of requests a given IP/Client/API Key has made across any number of App / serverless instances.

However, what do you do if the limit isn't roundable to the current second/minute/hour/day? How does each instance agree which key to use? The answer is to modulo the previous and future keys to find the right one.

For example, 3 requests every 5 seconds is the desired limit. That means we can use -max-1:+max-1 as the range of values to scan to find the key we should use.

max := 5