DSA SheetLesson · no judge

TIME AND SPACE COMPLEXITY / ONLINE JUDGEONLINE JUDGE

Tearing Down a Problem: Turn Every Sentence into a Contract

Reading · 7 minQuiz · 5 questions2 code drills · run onlyGenerated by gpt-5.6-luna · Aug 23

The output sentence decides what counts as a correct answer

The statement asks you to print the 0-based index of the first occurrence of x, not the value x itself. For the array [4, 2, 7, 2, 1] and x = 2, the value 2 appears at indices 1 and 3. The first occurrence is at index 1, so the required result is 1. Because indexing starts at 0, the first array element has index 0, making the answer 1 rather than 2.

The sample output is meaningful only when you connect it to every word in the required result. Printing 2 would print the target value, not its position. Printing 3 would print the position of a later match. Printing 1 is correct because it is the position of the first matching value under 0-based indexing.

the indexed array [4, 2, 7, 2, 1] searched for x = 2Five cells contain 4, 2, 7, 2, and 1, with indices 0 through 4 beneath them. The target is x = 2. The value at index 1 is highlighted as the first match, the value at index 3 is marked as a later match, and an arrow from index 1 points to "output 1".42721output 1x = 2first matchlater match01234
The first matching index, not the matching value, is printed.
CHECKPOINT 1Not answered

For the concrete array [4, 2, 7, 2, 1] and x = 2, what does the problem print?

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

The input format is a map from tokens to variables

Read the first line as two tokens in order: the token 5 becomes n, and the token 2 becomes x. The second line contains exactly n array values, so the five tokens become a[0] = 4, a[1] = 2, a[2] = 7, a[3] = 2, and a[4] = 1.

TEXTThe first two tokens bind n and x. The next n tokens bind the array from left to right.
5 2
4 2 7 2 1

Line breaks explain the input format to a human reader, but they do not change the token order. A typical C++ or Java input reader consumes whitespace, including spaces and newlines, in the same sequence. The program must still read n and x first, then read exactly n array values, because that order is the contract.

Constraints remove invalid assumptions before code exists

The constraint 1 <= n <= 100000 says the array is never empty and may be much larger than the concrete five-element sample. A solution must therefore treat n as the number of values to read, rather than assuming that five values always exist or reserving logic for only the shown sample.

The bounds -1000000000 <= a[i], x <= 1000000000 apply to every array value and to x, not just to the concrete values 4 and 2. Those bounds fit within a standard 32-bit signed integer range, so a C++ int or Java int can represent each input value. The constraint gives a storage fact, while n gives the number of values the input can contain.

CHECKPOINT 2Not answered

Type the variable binding produced by the concrete input, including n, x, and all five array values.

Checkpoints are not graded. They are here so you catch yourself before the quiz does — stuck, ask the tutor on the right.

A hand trace turns the statement into a coding checklist

Start at index 0 and compare the array value with x. At index 0, a[0] is 4, so the search continues. At index 1, a[1] is 2, which equals x, so the search stops immediately. The later match at index 3 is irrelevant because the first occurrence has already been found.

INDEXARRAY VALUECOMPARISON WITH XRESULT
044 != 2continue
122 == 2stop and print 1
The search stops at the first equal value.

The concrete input produces the exact output 1. Before writing code, turn the statement into four checks: read n and x in that order, read exactly n array values, stop at the first value equal to x, and print its 0-based index. If no value equals x, the required output is -1. The sample values cannot replace any of these rules, because a different valid input may have a different length, target, or matching position.

Previous · Popular Online PlatformsNext part · Quiz