Since I’ve been working with Go for quite some time, I thought it would be a fun challenge to implement a few classic low-level design solutions in it.
When designing an elevator system, one crucial aspect is how to decide which floor to service next, especially when the elevator has multiple requests. Go’s straightforward syntax and performance make it ideal for modeling such systems, so I set out to create basic implementations of FCFS (First Come First Serve), SSTF (Shortest Seek Time First), SCAN, and LOOK algorithms.
1. First Come First Serve (FCFS)
I started with the simplest approach: service requests in the order they’re received. It’s easy to implement but can be inefficient if the requests are spread out across floors, leading to more travel time.
func FCFS(currentFloor int, requests []int) []int { path := []int{} for _, floor := range requests { path = append(path, floor) } return path }
In FCFS, the elevator simply moves to each requested floor in the given order.
2. Shortest Seek Time First (SSTF)
SSTF tries to minimize travel by choosing the closest requested floor next. This reduces travel time but can lead to "starvation" for far-off requests if new closer requests keep coming.
func SSTF(currentFloor int, requests []int) []int { path := []int{} remaining := append([]int{}, requests...) for len(remaining) > 0 { closestIdx := 0 minDistance := abs(currentFloor - remaining[0]) for i, floor := range remaining { distance := abs(currentFloor - floor) if distance <p>This function finds the closest floor to the current floor each time, updating the elevator’s position after each move.</p> <h2> 3. SCAN (Elevator Algorithm) </h2> <p>In SCAN, the elevator moves in one direction, servicing all requests in that direction until it reaches the end, then reverses. This approach is more fair than SSTF because it reduces starvation.<br> </p> <pre class="brush:php;toolbar:false">func SCAN(currentFloor, maxFloor int, requests []int) []int { path := []int{} up := []int{} down := []int{} for _, floor := range requests { if floor >= currentFloor { up = append(up, floor) } else { down = append(down, floor) } } sort.Ints(up) sort.Sort(sort.Reverse(sort.IntSlice(down))) path = append(path, up...) path = append(path, down...) return path }
This function splits requests into floors above and below the current position. It serves all floors upwards, then downwards.
4. LOOK
LOOK is a slight variation of SCAN. Instead of going all the way to the end, the elevator reverses direction at the last request in each direction. It saves time by stopping where the requests end, not at the physical limits.
func LOOK(currentFloor int, requests []int) []int { path := []int{} up := []int{} down := []int{} for _, floor := range requests { if floor >= currentFloor { up = append(up, floor) } else { down = append(down, floor) } } sort.Ints(up) sort.Sort(sort.Reverse(sort.IntSlice(down))) path = append(path, up...) path = append(path, down...) return path }
Similar to SCAN, this approach only moves as far as the last request in each direction.
Each algorithm has its trade-offs:
- FCFS: Simple but can be inefficient.
- SSTF: Optimizes for closest floors but can starve far-off requests.
- SCAN: Fairer and efficient, minimizing direction changes.
- LOOK: Saves additional time by stopping at the last request.
The right choice depends on the specific requirements for efficiency, fairness, and response time in the system.
For full implementation using LOOK algorithm, refer to my github repo:
thesaltree
/
low-level-design-golang
Low level system design problems solutions in Golang
Low-Level System Design in Go
Welcome to the Low-Level System Design in Go repository! This repository contains various low-level system design problems and their solutions implemented in Go. The primary aim is to demonstrate the design and architecture of systems through practical examples.
Table of Contents
- Overview
- Parking Lot System
- Elevator System
Overview
Low-level system design involves understanding the core concepts of system architecture and designing scalable, maintainable, and efficient systems. This repository will try to cover solutions of various problems and scenarios using Go.
Parking Lot System
The first project in this repository is a Parking Lot System. This system simulates a parking lot where vehicles can be parked and unparked. It demonstrates:
- Singleton design pattern for managing the parking lot instance.
- Handling different types of vehicles (e.g., cars, trucks).
- Parking space management across multiple floors.
- Payment processing for parked vehicles.
Features
- Add and remove vehicles from the…
The above is the detailed content of Elevator Scheduling Algorithms: FCFS, SSTF, SCAN, and LOOK. For more information, please follow other related articles on the PHP Chinese website!

Mastering the strings package in Go language can improve text processing capabilities and development efficiency. 1) Use the Contains function to check substrings, 2) Use the Index function to find the substring position, 3) Join function efficiently splice string slices, 4) Replace function to replace substrings. Be careful to avoid common errors, such as not checking for empty strings and large string operation performance issues.

You should care about the strings package in Go because it simplifies string manipulation and makes the code clearer and more efficient. 1) Use strings.Join to efficiently splice strings; 2) Use strings.Fields to divide strings by blank characters; 3) Find substring positions through strings.Index and strings.LastIndex; 4) Use strings.ReplaceAll to replace strings; 5) Use strings.Builder to efficiently splice strings; 6) Always verify input to avoid unexpected results.

ThestringspackageinGoisessentialforefficientstringmanipulation.1)Itofferssimpleyetpowerfulfunctionsfortaskslikecheckingsubstringsandjoiningstrings.2)IthandlesUnicodewell,withfunctionslikestrings.Fieldsforwhitespace-separatedvalues.3)Forperformance,st

WhendecidingbetweenGo'sbytespackageandstringspackage,usebytes.Bufferforbinarydataandstrings.Builderforstringoperations.1)Usebytes.Bufferforworkingwithbyteslices,binarydata,appendingdifferentdatatypes,andwritingtoio.Writer.2)Usestrings.Builderforstrin

Go's strings package provides a variety of string manipulation functions. 1) Use strings.Contains to check substrings. 2) Use strings.Split to split the string into substring slices. 3) Merge strings through strings.Join. 4) Use strings.TrimSpace or strings.Trim to remove blanks or specified characters at the beginning and end of a string. 5) Replace all specified substrings with strings.ReplaceAll. 6) Use strings.HasPrefix or strings.HasSuffix to check the prefix or suffix of the string.

Using the Go language strings package can improve code quality. 1) Use strings.Join() to elegantly connect string arrays to avoid performance overhead. 2) Combine strings.Split() and strings.Contains() to process text and pay attention to case sensitivity issues. 3) Avoid abuse of strings.Replace() and consider using regular expressions for a large number of substitutions. 4) Use strings.Builder to improve the performance of frequently splicing strings.

Go's bytes package provides a variety of practical functions to handle byte slicing. 1.bytes.Contains is used to check whether the byte slice contains a specific sequence. 2.bytes.Split is used to split byte slices into smallerpieces. 3.bytes.Join is used to concatenate multiple byte slices into one. 4.bytes.TrimSpace is used to remove the front and back blanks of byte slices. 5.bytes.Equal is used to compare whether two byte slices are equal. 6.bytes.Index is used to find the starting index of sub-slices in largerslices.

Theencoding/binarypackageinGoisessentialbecauseitprovidesastandardizedwaytoreadandwritebinarydata,ensuringcross-platformcompatibilityandhandlingdifferentendianness.ItoffersfunctionslikeRead,Write,ReadUvarint,andWriteUvarintforprecisecontroloverbinary


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Zend Studio 13.0.1
Powerful PHP integrated development environment

Notepad++7.3.1
Easy-to-use and free code editor
