0
of 1900 pts
0
of 9 challenges
7
available
0%
completed
Code Challenges
Find the bug, fix the code and earn points. Use the Duck Mentor whenever you need help!
Language
Difficulty
This Python code tries to reverse a list, but it has a bug. Find the error!
def reverse_list(lst):
reversed_lst = []
for i in range(len(lst)):
reversed_lst.append(lst[i])
return reversed_lst
# Expected: [5, 4, 3, 2, 1]
# Result: [1, 2, 3, 4, 5]
print(reverse_list([1, 2, 3, 4, 5]))
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This JavaScript loop should print 0, 1, 2, but it prints 3, 3, 3. Why?
for (var i = 0; i < 3; i++) {
setTimeout(function() {
console.log(i);
}, 1000);
}
// Expected: 0, 1, 2
// Result: 3, 3, 3
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This TypeScript function has a type error. Find the problem!
interface User {
name: string;
age: number;
}
function getProperty<T>(obj: T, key: string) {
return obj[key]; // Error!
}
const user: User = { name: "Ana", age: 25 };
const name = getProperty(user, "name");
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This Java code compares strings the wrong way. What is the problem?
public class Main {
public static void main(String[] args) {
String a = new String("hello");
String b = new String("hello");
if (a == b) {
System.out.println("Equal!");
} else {
System.out.println("Different!");
}
// Prints: "Different!"
// Expected: "Equal!"
}
}
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This C# code throws a NullReferenceException. Where is the error?
public class UserService
{
private List<string> _users;
public void AddUser(string name)
{
_users.Add(name); // NullReferenceException!
}
public int GetCount()
{
return _users.Count;
}
}
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This C++ code has an overflow bug. Identify the problem!
#include <iostream>
using namespace std;
int factorial(int n) {
int result = 1;
for (int i = 1; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
cout << factorial(20) << endl;
// Result: -2102132736
// Expected: 2432902008176640000
return 0;
}
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This SQL query returns more rows than expected. What is the problem?
-- Tables: users (id, name), orders (id, user_id, total)
-- We want: total spent per user
SELECT u.name, SUM(o.total) as total_spent
FROM users u, orders o
WHERE u.id = o.user_id;
-- Missing GROUP BY! Incorrect result.
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This Python function has unexpected behavior with the default argument. Figure it out!
def add_item(item, lst=[]):
lst.append(item)
return lst
# Calls:
print(add_item("a")) # ['a'] - OK
print(add_item("b")) # ['a', 'b'] - Bug!
print(add_item("c")) # ['a', 'b', 'c'] - Bug!
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix:
This async function doesn't wait for the result correctly. Where is the bug?
async function fetchUsers() {
const userIds = [1, 2, 3, 4, 5];
const users = [];
userIds.forEach(async (id) => {
const response = await fetch(`/api/users/${id}`);
const user = await response.json();
users.push(user);
});
console.log(users); // [] - Empty array!
return users;
}
Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.
Select the fix: