Computing diagnostic and gap-specific repair map
Attempt these without running code or opening solutions first. Then verify executable predictions where appropriate. “Not learned yet” is a valid response. These twelve tasks complement the sixteen mathematics diagnostic tasks; a total score is less useful than the pattern of errors. Do not execute code that the question identifies as having undefined behavior.
Trace total=0 followed by a loop over [2,-1,3] that performs total=total+value. Give the value after each iteration and explain what assignment means.
Predict the final lists after a=[1,2]; b=a; b.append(3); b=[9]. Explain which statement mutates a list and which rebinds a name.
For sorted data [1,3,3,8], what should a function returning the first index with value at least 4 return? What should it return on an empty list if the contract uses the length as the no-match result? Explain without relying on a library call.
A C++ function returns a pointer to a local automatic integer. Why is a non-null returned pointer not enough to make later dereferencing safe?
Is “if P then Q; Q is true; therefore P” valid? Supply a truth assignment that decides the question.
Negate “every input has some valid output.” Distinguish it from “there is one output valid for every input.”
An algorithm solves two half-sized subproblems and then scans all elements. Explain the recurrence and the work per level for powers-of-two input sizes.
Insert 1, then 2, then 3 into an empty stack and an empty queue. Give the first removed item from each and identify one suitable use of each abstraction.
Two distinct keys have the same hash. Must one overwrite the other? Explain what a correct hash table must do.
Interpret eight one-bits as an unsigned integer and as a two’s-complement signed integer. Explain why mathematical integers and a bounded machine type differ.
Two C++ threads update one ordinary shared counter without synchronization. Is running the program successfully 1,000 times proof that it is safe? Identify the relevant issue.
A service subtracts stock in one committed statement and later stores the request identifier in another. A crash occurs between them, and the client retries. Identify the missing atomic unit and the possible duplicate effect.
Use the error pattern#
| Tasks | What the error may indicate | Repair path |
|---|---|---|
| CD.01–02 | State updates, bindings, aliasing | C01; trace before execution |
| CD.03 | Contract and boundary interpretation | C02; half-open search interval |
| CD.04 | Lifetime versus pointer value | C03; draw ownership and scope |
| CD.05–06 | Implication, converse, quantifier order | C04; construct countermodels |
| CD.07 | Recurrence disconnected from operations | C05 and C07; count each level |
| CD.08–09 | Abstract operations versus representation | C06; trace actual contents |
| CD.10 | Representation and domain limits | C09; decode bits explicitly |
| CD.11 | Testing confused with concurrent correctness | C10; identify synchronization |
| CD.12 | Transaction and retry boundaries | C11–C13; trace commit and lost reply |
An excellent trace does not automatically establish implementation skill. Pair it with C01.05 or another unseen lab. Conversely, a strong independent implementation and explanation can justify moving past routine beginner syntax. Treat the diagnostic as evidence about particular abilities, not a label for your entire background.
Complete solutions: Computing diagnostic#
Compare the first step where your work differs. A corrected answer is not yet an independent success.
CD.01Show solution
One evolving state
The totals are 2, 1, and 4. Each right-hand expression uses the old total and current value, then assignment replaces the binding. It is not an algebraic assertion that the old and new total are simultaneously equal. A useful invariant is that the total equals the sum of inputs processed so far.
Back to CD.01CD.02Show solution
Mutation versus rebinding
After b=a, both names refer to the same list. Appending 3 mutates that shared list, so a now refers to [1,2,3]. The later assignment b=[9] binds b to a new list without changing a’s list. Final results are a=[1,2,3] and b=[9]. Draw two name arrows before and after rebinding to verify the distinction.
CD.03Show solution
The insertion boundary is a valid result
Index 3 is the first location whose value, 8, is at least 4. On an empty sequence the length is zero, so the result is zero without an indexed read. The result may equal the length; callers must distinguish an insertion position from an existing-element index. Sortedness and the exact inequality are part of the function’s precondition and postcondition.
Back to CD.03CD.04Show solution
An address does not keep an object alive
The local integer’s lifetime ends when the function returns. Its former address can remain non-null as a pointer value, but reading through it is not a valid access to a live object. Return a value or provide an owner with a suitable lifetime and interface. A non-null check addresses only one invalid-pointer case, not dangling lifetime.
Back to CD.04CD.05Show solution
Affirming the consequent fails
Set P false and Q true. The implication PQ is true and Q is true, while the conclusion P is false. This is a countermodel, proving the inference invalid in propositional logic. The valid converse-looking rule would require an additional premise QP, which was not supplied.
Back to CD.05CD.06Show solution
Negate from the outside inward
The first claim is . Its negation is : some input has no valid output. The second claim is , requiring one common output for all inputs. A choice that depends on the input is not necessarily a single common choice.
Back to CD.06CD.07Show solution
Count a complete recursion level
A model is with a constant-size base case. Each level’s subproblem sizes sum to , so its scanning work is . There are internal levels for powers of two, giving total work plus linear leaf cost. This counts operations under the model; it does not measure a specific implementation’s wall time.
Back to CD.07CD.08Show solution
Removal order defines the abstraction
The stack first removes 3, the most recently inserted item. The queue first removes 1, the earliest remaining item. Nested call processing or depth-first traversal suits a stack; breadth-first traversal or FIFO work arrival suits a queue. Different data structures can implement the same abstraction, with different performance and lifetime properties.
Back to CD.08CD.09Show solution
A collision is not key equality
A correct hash table retains both distinct keys and resolves the collision, for example with a chain or a probe sequence. Lookup compares actual keys rather than treating equal hashes as identical keys. Replacing one value is correct only when the key-equality relation says it is the same key. Collisions affect performance but need not violate correctness.
Back to CD.09CD.10Show solution
The representation changes the interpretation
Eight one-bits represent 255 unsigned and in eight-bit two’s complement. Mathematical integers have no fixed largest or smallest value, whereas an eight-bit representation has only 256 possible patterns. Arithmetic and overflow behavior depend on the language and type, not merely on the mathematical intention.
Back to CD.10CD.11Show solution
Repeated success does not remove a data race
Unsynchronized conflicting non-atomic accesses can be a C++ data race, giving undefined behavior. A thousand successful executions explore only some schedules and compiler/hardware circumstances. Establish an appropriate mutex or atomic protocol and justify its invariant and object lifetimes. Stress tests are additional evidence, not a proof of race freedom.
Back to CD.11CD.12Show solution
The business effect and retry record must commit together
If stock subtraction commits but the request identifier does not, a retry can look new and subtract again. The effect and the durable deduplication record must be part of one transaction, with the request key tied to its payload and recorded result. A crash before commit should leave neither; a retry after commit should retrieve the original result. J03 implements this bounded pattern in SQLite while explicitly limiting its production claims.
Back to CD.12