TryBugHunt
javaOtherMedium

Booked seat is reported as free

BugFind the bug and fix the code

What it should do

isBooked(booked, seat) returns true when the seat number is in the booked array, so isBooked([4, 7, 12], 7) returns true.

What happens instead

isBooked([4, 7, 12], 7) returns false. It returns false for every seat.

import java.util.*;

public class Main {
    static boolean isBooked(int[] booked, int seat) {
        return Arrays.asList(booked).contains(seat);
    }
}

Test a custom input to verify an edge case (e.g. empty lists, negative numbers, duplicates).

⏱️0:00
Hints
🏆 0 hints = Flawless Solve
Think & Test Cooldown
2:00 remaining

Take a moment to read the bug symptom and trace the code with Run & Check before unlocking Hint 1.

Hint 2
Unlocks after Hint 1
Hint 3
Unlocks after Hint 2

This bug belongs to the mutation and copying bugs pattern

Two references to one object, so changing one changes both; shallow copies of lists.

What you'll practise

A medium Java debugging exercise about mutation and copying bugs: two references to one object, so changing one changes both; shallow copies of lists.

The checks your fix must pass

Signs of this kind of bug

Spoiler: why this bug happens(try it first; the hints get warmer one at a time)

Arrays.asList takes a variable number of objects. An int[] is a single object, and its int elements cannot be boxed one by one, so Arrays.asList(booked) creates a List<int[]> with exactly one element: the whole array. contains(7) then asks whether that list holds the Integer 7, which it never does. The code compiles without a warning because contains accepts any Object. Arrays.asList works as expected on Integer[] and String[], but not on primitive arrays. For primitives, use Arrays.stream(booked).anyMatch(...) or a plain loop.

The pattern behind it: mutation and copying bugs.

More mutation and copying bugs to practise

Related errors

Embed this challenge on your site

Paste this into any web page or blog post. Visitors fix the bug right there, in their browser.

<iframe src="https://trybughunt.com/embed/080ae6e5-9c23-41dc-9af6-624b30b2d7a0" title="Debugging challenge: Booked seat is reported as free" width="100%" height="580" loading="lazy" style="border:1px solid #d4d4d8;border-radius:12px;max-width:760px"></iframe>
<p style="font-size:14px">Debugging challenge from <a href="https://trybughunt.com/challenges/080ae6e5-9c23-41dc-9af6-624b30b2d7a0">TryBugHunt</a></p>