Brain Out Level 190
๐ Walkthrough
Overview
Level 190 shows a Java-style selection-sort method on screen, with the question "a=?" at the top. The code declares var a, b, c;, then inside the outer loop sets int m = i; b = 10;, and inside the inner loop says if (number[j] < number[m]) a = b;. Most players try to trace the whole sort, tracking the array and every swap, but the Brain Out trick is to ignore the algorithm. The only assignment that touches a is a = b, and b is hard-coded to 10. So whenever a gets assigned, it gets 10. The answer to type is 10.
- Difficulty: Regular
- Estimated moves: 2
Step-by-Step Solution
- Read the question at the top: it asks "a=?" You are not being asked to run the sort or output the array; you are being asked for the value of variable
a. - Scan the code from top to bottom. The declaration
var a, b, c;introduces the variables but does not initialize them. - Find every place where
ais assigned. There is exactly one such line:a = b;inside theif (number[j] < number[m])block. - Find every place where
bis assigned. There is exactly one such line:b = 10;at the top of the outer loop. - Since
a = bandb = 10, the value written intoais 10. The rest of the sort logic, themvariable, and theswapcall do not affecta. - Tap the yellow Enter field and type the digit 10. Tap the blue OK button. The level-complete pop-up appears with the line "Don't be tricked!" Tap Next to continue.
- The code on the screen assigns b equal to ten, and a is equal to b. Therefore a equals ten.
Strategy & Reasoning
The puzzle is a code-reading trick. The selection-sort body looks complex on purpose, with nested loops, an index variable m, and a swap call. A normal programmer would start tracing the array and tracking comparisons, but the question only asks about a. The shortest path is to follow the data flow backward from a to its only assignment.
Variable a is never initialized at declaration. It is only written in one place, inside the if. At that place, the right-hand side is b, and b is assigned 10 at the start of every outer iteration. So no matter how many times the if runs, a receives 10 each time. The rest of the algorithm is noise.
The congratulatory line "Don't be tricked!" is the game laughing at the people who tried to simulate the sort. The lesson is to read the specific variable being asked about rather than diving into the whole routine. Brain Out's code puzzles are almost always solved by tracing one variable to its literal value, not by executing the algorithm.
Tips & Common Mistakes
- Trying to simulate the selection sort by hand. You do not need the array contents; only the assignment to
amatters. - Entering 0 or null because
ais uninitialized. Theifblock assignsa = b, so once the code runs,aholds 10. - Confusing
b = 10with a typo or a loop counter.bis a plain variable set to 10, andacopies it.