In-class bugs exercise 1
Backend Bug Review Exercises
Review each backend code sample, identify the obvious bug, explain what the program is trying to do, and describe how you would fix it during a technical interview.
Exercise 1 · Fix the Login Function
Python · Beginnerdef login_user(username, password):
saved_password = "admin123"
if password != saved_password:
return "Login successful"
else:
return "Incorrect password"
print(login_user("robbie", "admin123"))
🎯 Instructions bug review
- Explain what the function is supposed to do.
- Identify the incorrect line.
- Explain what the operator
!=means. - Describe the current output.
- Explain how to correct the condition.
- Read the corrected function out loud.
📖 Vocabulary core
- authentication — checking whether a user is allowed to access a system.
- stored password — the password saved by the application.
- comparison operator — an operator that compares two values.
- logic bug — code that runs but produces the wrong result.
- return value — the value sent back by a function.
- condition — an expression evaluated as true or false.
🧩 Collocations natural English
- check a password
- compare two values
- identify a logic bug
- return an error message
- grant access
- reject a login attempt
🗣️ Phrasal Verbs interview speech
- log in — “The user is trying to log in.”
- check against — “The password is checked against the stored password.”
- send back — “The function sends back a message.”
- switch around — “The two outcomes have been switched around.”
🐞 Bug Explanation answer
The condition uses !=, which means “not equal to.” The code currently returns
"Login successful" when the entered password is different from the saved password.
Because "admin123" is equal to the saved password, the condition is false and the
function incorrectly returns "Incorrect password".
✅ Corrected Code solution
def login_user(username, password):
saved_password = "admin123"
if password == saved_password:
return "Login successful"
else:
return "Incorrect password"
print(login_user("robbie", "admin123"))
🎤 Model Answer spoken
This function is intended to check whether a user entered the correct password. The entered password is compared with a stored password.
The bug is in the if condition. It uses the not-equal operator, so the program
reports a successful login when the passwords are different.
I would replace != with ==. After this change, the function returns
“Login successful” when the entered password matches the stored password.
Exercise 2 · Fix the User API Route
JavaScript · Expressconst express = require("express");
const app = express();
const users = {
1: { id: 1, name: "Anna" },
2: { id: 2, name: "David" }
};
app.get("/users/:id", (req, res) => {
const userId = req.params.userId;
const user = users[userId];
res.json(user);
});
app.listen(3000);
🎯 Instructions bug review
- Explain what the route is supposed to return.
- Identify the route parameter declared in the URL.
- Identify the incorrect property name.
- Explain why
userIdbecomes undefined. - Describe how to fix the bug.
- Explain what should happen for a request to
/users/1.
📖 Vocabulary core
- API route — a backend URL that handles a request.
- route parameter — a variable value included in a URL.
- request object — an object containing information about the incoming request.
- response body — the data returned to the client.
- undefined — a JavaScript value indicating that no value was found.
- object lookup — retrieving a value from an object using a key.
🧩 Collocations natural English
- define a route
- read a parameter
- retrieve a user
- send a JSON response
- match a parameter name
- handle a request
🗣️ Phrasal Verbs interview speech
- look up — “The route looks up the user by ID.”
- send back — “The server sends back a JSON object.”
- come in — “The ID comes in through the URL.”
- line up — “The parameter names need to line up.”
🐞 Bug Explanation answer
The URL declares a parameter called id in /users/:id.
However, the route tries to read req.params.userId.
Since there is no parameter called userId, the value is undefined.
The program then tries to retrieve users[undefined], so no user is returned.
✅ Corrected Code solution
const express = require("express");
const app = express();
const users = {
1: { id: 1, name: "Anna" },
2: { id: 2, name: "David" }
};
app.get("/users/:id", (req, res) => {
const userId = req.params.id;
const user = users[userId];
res.json(user);
});
app.listen(3000);
🎤 Model Answer spoken
This Express route retrieves a user by an ID included in the URL.
The route declares the parameter as id.
The bug occurs because the code tries to read req.params.userId.
That property does not exist, so the value becomes undefined.
I would change it to req.params.id. Then a request to
/users/1 will return the user named Anna as JSON.
Exercise 3 · Fix the Product Creation Endpoint
Python · Flaskfrom flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/products", methods=["POST"])
def create_product():
product_data = request.get_json()
name = data["name"]
price = data["price"]
return jsonify({
"message": "Product created",
"name": name,
"price": price
}), 201
app.run(debug=True)
🎯 Instructions bug review
- Explain what the endpoint is supposed to do.
- Identify the variable that stores the JSON request data.
- Identify the variable name that causes the crash.
- Name the likely Python error.
- Explain how to correct both lookup lines.
- Explain the meaning of the HTTP status code
201.
📖 Vocabulary core
- endpoint — a backend URL that performs a specific action.
- request body — data sent by the client to the server.
- JSON payload — structured JSON data included in a request.
- variable name — the identifier used to store and access a value.
- NameError — a Python error caused by using a name that is not defined.
- status code — a number indicating the result of an HTTP request.
🧩 Collocations natural English
- receive a request
- parse JSON data
- create a product
- raise an exception
- return a response
- use a consistent variable name
🗣️ Phrasal Verbs interview speech
- come in — “The product data comes in as JSON.”
- pull out — “The endpoint pulls out the name and price.”
- break down — “The request breaks down because the variable is undefined.”
- send back — “The endpoint sends back a success response.”
🐞 Bug Explanation answer
The JSON request is saved in a variable called product_data.
However, the next two lines try to access a variable called data.
Since data was never defined, Python raises a NameError.
The code should use product_data consistently.
✅ Corrected Code solution
from flask import Flask, request, jsonify
app = Flask(__name__)
@app.route("/products", methods=["POST"])
def create_product():
product_data = request.get_json()
name = product_data["name"]
price = product_data["price"]
return jsonify({
"message": "Product created",
"name": name,
"price": price
}), 201
app.run(debug=True)
🎤 Model Answer spoken
This Flask endpoint receives product information from a POST request.
It uses request.get_json() to convert the JSON request body into a Python dictionary.
The bug is caused by inconsistent variable names. The request data is stored in
product_data, but the code later tries to use data.
I would replace both references to data with product_data.
The endpoint can then return the created product with status code 201,
which means that a new resource was created successfully.