Bird
Raised Fist0
FastAPIframework~20 mins

Folder structure patterns in FastAPI - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
FastAPI Folder Structure Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Understanding FastAPI folder structure for scalability
Which folder structure below best supports adding multiple API routers and models in a FastAPI project for better scalability?
A
app/
  main.py
  api/
    v1/
      endpoints.py
      models.py
  core/
    config.py
  utils.py
B
app/
  main.py
  models.py
  routes.py
  utils.py
C
app/
  main.py
  api.py
  models.py
  helpers.py
D
app/
  main.py
  endpoints/
    users.py
    items.py
  models.py
Attempts:
2 left
💡 Hint
Think about how to organize code to easily add new API versions and separate concerns.
❓ component_behavior
intermediate
2:00remaining
Behavior of FastAPI app with nested routers
Given this folder structure and code, what will be the URL path to access the 'get_users' endpoint? Structure: app/ main.py api/ v1/ users.py Code in users.py: from fastapi import APIRouter router = APIRouter(prefix='/users') @router.get('/') async def get_users(): return ['Alice', 'Bob'] Code in main.py: from fastapi import FastAPI from api.v1.users import router as users_router app = FastAPI() app.include_router(users_router, prefix='/v1')
FastAPI
from fastapi import APIRouter
router = APIRouter(prefix='/users')

@router.get('/')
async def get_users():
    return ['Alice', 'Bob']

from fastapi import FastAPI
from api.v1.users import router as users_router
app = FastAPI()
app.include_router(users_router, prefix='/v1')
A/users/
B/v1/users/
C/users/v1/
D/v1/
Attempts:
2 left
💡 Hint
Remember how prefixes combine when including routers.
📝 Syntax
advanced
2:00remaining
Identify the syntax error in FastAPI folder import
In a FastAPI project with this folder structure: app/ main.py api/ __init__.py v1/ __init__.py users.py Which import statement in main.py correctly imports the 'users' router?
Aimport api.v1.users.router
Bfrom api/v1/users import router
Cfrom api.v1.users import router
Dfrom api.v1 import users.router
Attempts:
2 left
💡 Hint
Python imports use dots, not slashes.
🔧 Debug
advanced
2:00remaining
Debugging missing endpoint error in FastAPI project
You have this folder structure: app/ main.py api/ v1/ users.py In users.py: from fastapi import APIRouter router = APIRouter(prefix='/users') @router.get('/users') async def read_users(): return ['user1', 'user2'] In main.py: from fastapi import FastAPI from api.v1 import users app = FastAPI() app.include_router(users.router, prefix='/v1') When you run the app and visit '/v1/users', you get 404 Not Found. What is the most likely cause?
AThe router in users.py has prefix '/users', so the endpoint is at '/v1/users/users' instead of '/v1/users'.
BThe users.py file is missing an __init__.py, so the import fails silently.
CThe endpoint function read_users is missing the @app.get decorator.
DThe include_router call should not have a prefix argument.
Attempts:
2 left
💡 Hint
Check how prefixes combine in routers.
❓ state_output
expert
2:00remaining
Output of FastAPI app with multiple routers and prefixes
Consider this FastAPI app folder structure: app/ main.py api/ v1/ users.py items.py users.py: from fastapi import APIRouter router = APIRouter(prefix='/users') @router.get('/') async def get_users(): return ['user1'] items.py: from fastapi import APIRouter router = APIRouter(prefix='/items') @router.get('/') async def get_items(): return ['item1'] main.py: from fastapi import FastAPI from api.v1.users import router as users_router from api.v1.items import router as items_router app = FastAPI() app.include_router(users_router, prefix='/v1') app.include_router(items_router, prefix='/v1') What will be the JSON response when a client requests GET /v1/items/ ?
FastAPI
from fastapi import APIRouter

# users.py
router = APIRouter(prefix='/users')
@router.get('/')
async def get_users():
    return ['user1']

# items.py
router = APIRouter(prefix='/items')
@router.get('/')
async def get_items():
    return ['item1']

# main.py
from fastapi import FastAPI
from api.v1.users import router as users_router
from api.v1.items import router as items_router
app = FastAPI()
app.include_router(users_router, prefix='/v1')
app.include_router(items_router, prefix='/v1')
A['user1', 'item1']
B['user1']
C404 Not Found error
D['item1']
Attempts:
2 left
💡 Hint
Check how prefixes combine for each router and which endpoint is called.

Practice

(1/5)
1. Which folder is best suited to keep your API endpoint definitions in a FastAPI project?
easy
A. routes
B. models
C. services
D. database

Solution

  1. Step 1: Understand folder roles in FastAPI

    The routes folder is typically used to store API endpoint definitions, organizing the paths and HTTP methods.
  2. Step 2: Match folder purpose to content

    models is for data models, services for business logic, and database for database connection code.
  3. Final Answer:

    routes -> Option A
  4. Quick Check:

    API endpoints go in routes [OK]
Hint: API paths belong in routes folder [OK]
Common Mistakes:
  • Putting endpoints in models folder
  • Mixing database code with routes
  • Storing business logic in routes
2. Which of the following is the correct way to organize a FastAPI project folder for database models?
easy
A. Store models in the root project folder
B. Place all models inside a folder named services
C. Keep models mixed with route files
D. Put database models inside a folder named models

Solution

  1. Step 1: Identify the folder for data models

    Database models represent data structure and belong in a dedicated models folder for clarity and separation.
  2. Step 2: Evaluate other options

    services is for business logic, mixing models with routes or root folder reduces clarity and maintainability.
  3. Final Answer:

    Put database models inside a folder named models -> Option D
  4. Quick Check:

    Models folder holds data models [OK]
Hint: Models go in models folder, not services or root [OK]
Common Mistakes:
  • Confusing services with models
  • Mixing models and routes
  • Scattering models in root folder
3. Given this FastAPI folder structure:
app/
  routes/
    user.py
  models/
    user.py
  services/
    user_service.py

Which file should contain the function to fetch user data from the database?
medium
A. routes/user.py
B. models/user.py
C. services/user_service.py
D. app/__init__.py

Solution

  1. Step 1: Understand folder responsibilities

    routes handles API endpoints, models defines data structures, and services contains business logic like fetching data.
  2. Step 2: Match function purpose to folder

    Fetching user data is business logic, so it belongs in services/user_service.py.
  3. Final Answer:

    services/user_service.py -> Option C
  4. Quick Check:

    Data fetching logic goes in services [OK]
Hint: Business logic like fetching data belongs in services [OK]
Common Mistakes:
  • Putting fetching logic in routes
  • Adding functions to models instead of services
  • Using __init__.py for logic
4. You see this folder structure in a FastAPI project:
app/
  routes.py
  models.py
  services.py
  database.py

What is a likely problem with this structure as the project grows?
medium
A. Too many files in root, making it hard to maintain
B. Using .py files instead of folders is invalid in FastAPI
C. Missing a templates folder for HTML rendering
D. Database code should be inside routes.py

Solution

  1. Step 1: Analyze flat file structure

    Having all files directly under app/ is okay for small projects but becomes cluttered and hard to maintain as code grows.
  2. Step 2: Understand folder benefits

    Using folders like routes/, models/, and services/ helps organize code by purpose and improves maintainability.
  3. Final Answer:

    Too many files in root, making it hard to maintain -> Option A
  4. Quick Check:

    Flat structure hurts maintainability [OK]
Hint: Avoid many files in root; use folders for organization [OK]
Common Mistakes:
  • Thinking .py files can't be used
  • Confusing database code location
  • Assuming templates folder is mandatory
5. You want to organize a FastAPI project that has multiple features like users, products, and orders. Which folder structure pattern best supports scalability and maintainability?
hard
A. Mix routes and models in the same folder to reduce folder count
B. Create separate folders for each feature with their own routes, models, and services inside
C. Keep all code files in the root folder for easy access
D. Put all routes in one routes folder, all models in one models folder, and all services in one services folder

Solution

  1. Step 1: Understand feature-based folder structure

    Organizing by feature means each feature (users, products, orders) has its own folder containing routes, models, and services, improving modularity.
  2. Step 2: Compare with other patterns

    Grouping all routes or models together can become large and hard to maintain. Keeping all files in root or mixing unrelated code reduces clarity.
  3. Final Answer:

    Create separate folders for each feature with their own routes, models, and services inside -> Option B
  4. Quick Check:

    Feature-based folders improve scalability [OK]
Hint: Use feature folders with routes, models, services inside [OK]
Common Mistakes:
  • Using flat folders for all code
  • Mixing unrelated code in one folder
  • Ignoring modular design for growth
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard