A Go backend service that optimizes package delivery by minimizing over-delivery while using configurable fixed-size packages.
Given a requested quantity and a set of available package sizes, the service calculates the optimal combination of packages that:
- Primary goal: Minimizes over-delivery (total_delivered - requested)
- Secondary goal: Minimizes the total number of packages used (when over-delivery is tied)
- HTTP API endpoint for package optimization calculations
- Configurable package sizes via environment variables
- Comprehensive unit tests
- Docker containerization
- Simple web UI for testing
- Well-structured codebase with separate layers
- Built with Echo framework for high performance
- Clone the repository:
git clone <repository-url>
cd package-optimizer
- Run with Docker Compose:
docker-compose up --build
- Access the service:
-
Install Go 1.21+ and ensure it's in your PATH
-
Set package sizes (optional, defaults to 250,500,1000,2000):
export PACKAGE_SIZES="250,500,1000,2000"
- Run the service:
go run cmd/server/main.go
- Run tests:
go test ./...
Endpoint: GET /calculate?qty={quantity}
Example:
curl "http://localhost:8080/calculate?qty=1201"
Response:
{
"requested": 1201,
"total_delivered": 1250,
"over_delivery": 49,
"packages": {
"250": 5
}
}
PACKAGE_SIZES
: Comma-separated list of available package sizes (default: "250,500,1000,2000")PORT
: Server port (default: 8080)
export PACKAGE_SIZES="100,200,500,1000"
export PORT=3000
package-optimizer/
├── cmd/
│ └── server/
│ └── main.go # Application entry point
├── internal/
│ ├── api/
│ │ ├── handler.go # HTTP handlers (Echo framework)
│ │ └── middleware.go # HTTP middleware (Echo framework)
│ ├── domain/
│ │ ├── optimizer.go # Core optimization logic
│ │ └── types.go # Domain types
│ └── config/
│ └── config.go # Configuration management
├── web/
│ └── static/
│ ├── index.html # Web UI
│ ├── style.css # CSS styles
│ └── script.js # JavaScript logic
├── tests/
│ └── optimizer_test.go # Unit tests
├── Dockerfile # Docker configuration
├── docker-compose.yml # Docker Compose setup
├── go.mod # Go module definition
├── go.sum # Go dependencies checksum
└── README.md # This file
go test ./...
go test -cover ./...
go test ./tests/optimizer_test.go
docker build -t package-optimizer .
docker run -p 8080:8080 package-optimizer
docker run -p 8080:8080 -e PACKAGE_SIZES="100,200,500" package-optimizer
The optimization algorithm uses a dynamic programming approach:
- State:
dp[i]
represents the minimum over-delivery for quantityi
- Transition: For each package size, try using it and update the minimum over-delivery
- Tie-breaking: When over-delivery is equal, prefer fewer packages
- O(n × m) where n is the requested quantity and m is the number of package sizes
- Space complexity: O(n)
- Zero quantity (returns empty result)
- Negative quantity (returns error)
- Very large quantities (handled efficiently)
- Invalid package sizes (validated)
- Empty package sizes list (returns error)
- Fork the repository
- Create a feature branch
- Make your changes
- Add tests for new functionality
- Ensure all tests pass
- Submit a pull request
This project is licensed under the MIT License - see the LICENSE file for details.
- ✅ Commercial Use: Allowed
- ✅ Modification: Allowed
- ✅ Distribution: Allowed
- ✅ Private Use: Allowed
- ❌ Liability: Limited
- ❌ Warranty: Limited
The MIT License is a permissive license that allows others to use, modify, and distribute your code with very few restrictions.