πŸš€ Sliding Window API

A production-ready FastAPI-based REST API implementing high-performance array and stack/deque algorithms such as Sliding Window, Monotonic Stack, and Histogram problems.

This project focuses on clean architecture, O(n) optimized solutions, and real-world backend deployment.


πŸ“Œ Features

  • Optimized algorithms with O(n) time complexity
  • FastAPI with automatic Swagger documentation
  • Core DSA concepts: Stack, Deque, HashMap
  • Fully deployable on Render
  • Clean and modular code structure

βš™οΈ Installation & Run

1) Clone the repository
git clone https://github.com/anshkunj/sliding-window-api
cd sliding-window-api

2) Install dependencies
pip install -r requirements.txt

3) Run the server
uvicorn main:app --reload


🌐 API Documentation

Swagger UI: http://127.0.0.1:8000/docs

ReDoc: http://127.0.0.1:8000/redoc


🌐 Live API

Base URL:
https://sliding-window-api.onrender.com
Docs:
https://sliding-window-api.onrender.com/docs


πŸ—‚ repo Structure

sliding-window-api/  
β”œβ”€β”€ main.py          # FastAPI app & routes
β”œβ”€β”€ logic.py         # Core algorithm implementations  
β”œβ”€β”€ models.py        # Pydantic request models  
β”œβ”€β”€ .gitignore  
β”œβ”€β”€ requirements.txt  
β”œβ”€β”€ render.yaml  
β”œβ”€β”€ README.md         # Project Overview  
└── LICENSE           # Licence file (MIT)  

🧠 Algorithms Implemented

0️⃣ Health

Check status of service whether it is live or not.


1️⃣ Next Greater Element

Find the next greater element to the right of each element.

Example: Input: [4, 2, 1, 5]
Output: [5, 5, 5, -1]


2️⃣ Daily Temperatures

Return the number of days until a warmer temperature.

Example: Input: [73, 74, 75, 71, 69, 72, 76, 73]
Output: [1, 1, 4, 2, 1, 1, 0, 0]


3️⃣ Largest Rectangle in Histogram

Find the maximum rectangular area in a histogram.

Example: Input: [2, 1, 5, 6, 2, 3]
Output: 10


4️⃣ Maximum Sliding Window

Find the maximum element in each sliding window of size k.

Example: Input: [2, 1, 3, 4, 6, 3, 8, 9, 10, 12, 56], k = 4
Output: [4, 6, 6, 8, 9, 10, 12, 56]


πŸ”Œ API Endpoints

GET /health
GET /next_greater
POST /next_greater
GET /daily_temperatures
POST /daily_temperatures
GET /largest_rectangle
POST /largest_rectangle
GET /max_sliding_window
POST /max_sliding_window


🧾 Request Format (JSON)

{ "arr": [1, 3, 2, 4], "k": 2 }


πŸ“€ Sample Response

{ "result": [3, 4, 4, -1] }


▢️ Run Locally

pip install -r requirements.txt
uvicorn main:app --reload

Open in browser:
http://127.0.0.1:8000/docs


☁️ Deployment (Render)

Build Command:
pip install -r requirements.txt

Start Command:
uvicorn main:app --host 0.0.0.0 --port 10000


πŸ›  Tech Stack

  • Python 3.10+
  • FastAPI
  • Pydantic
  • Uvicorn

🎯 Learning Outcomes

  • Mastery of Monotonic Stack
  • Sliding Window optimization
  • DSA + Backend integration
  • Real-world API deployment experience

πŸ“„ License

MIT License


🀝 Contributing

Contributors are welcome!
β€’ Add new sliding window problems
β€’ Improve explanations
β€’ Optimise exists code


πŸ‘€ Author

anshkunj

πŸ“« Let’s connect


⭐ Support

If you found this project helpful, give it a star ⭐
It motivates me to build more real-world APIs πŸš€


πŸ”Ή Note

This repository is regularly updated with new scripts and improvements.

Built With

  • algorithms
  • api
  • automation
  • backend
  • bug-fixing
  • data-structures
  • fastapi
  • json
  • problem-solving
  • python
  • workflow-optimisation
Share this project:

Updates