Score

0

of 1900 pts

Completed

0

of 9 challenges

Languages

7

available

Progress

0%

completed

Code Challenges

Find the bug, fix the code and earn points. Use the Duck Mentor whenever you need help!

Language

Difficulty

EasyPython
Reversed List

This Python code tries to reverse a list, but it has a bug. Find the error!

100pts
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]))
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

MediumJavaScript
Closure Trap

This JavaScript loop should print 0, 1, 2, but it prints 3, 3, 3. Why?

200pts
for (var i = 0; i < 3; i++) {
  setTimeout(function() {
    console.log(i);
  }, 1000);
}
// Expected: 0, 1, 2
// Result: 3, 3, 3
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

MediumTypeScript
Wrong Generic Type

This TypeScript function has a type error. Find the problem!

200pts
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");
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

EasyJava
String Comparison

This Java code compares strings the wrong way. What is the problem?

100pts
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!"
    }
}
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

MediumC#
Null Reference Exception

This C# code throws a NullReferenceException. Where is the error?

200pts
public class UserService
{
    private List<string> _users;

    public void AddUser(string name)
    {
        _users.Add(name); // NullReferenceException!
    }

    public int GetCount()
    {
        return _users.Count;
    }
}
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

HardC++
Silent Overflow

This C++ code has an overflow bug. Identify the problem!

300pts
#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;
}
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

MediumSQL
Incorrect JOIN

This SQL query returns more rows than expected. What is the problem?

200pts
-- 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.
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

HardPython
Mutable Default

This Python function has unexpected behavior with the default argument. Figure it out!

300pts
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!
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix:

HardJavaScript
Lost Promise

This async function doesn't wait for the result correctly. Where is the bug?

300pts
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;
}
Duck Mentor
Duck MentorRubber Duck Debugging

Need help? Explain the code to me and I'll give you a hint! Each hint costs 25 penalty points.

Select the fix: