Foundations to FrontierPart I: Start hereBezhig

Computing diagnostic and gap-specific repair map

12 exercises, each with a complete solution.

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.

CD.01FSolution

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.

CD.02FSolution

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.

CD.03CSolution

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.

CD.04CSolution

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?

CD.05CSolution

Is “if P then Q; Q is true; therefore P” valid? Supply a truth assignment that decides the question.

CD.06CSolution

Negate “every input has some valid output.” Distinguish it from “there is one output valid for every input.”

CD.07CSolution

An algorithm solves two half-sized subproblems and then scans all nn elements. Explain the recurrence and the work per level for powers-of-two input sizes.

CD.08FSolution

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.

CD.09CSolution

Two distinct keys have the same hash. Must one overwrite the other? Explain what a correct hash table must do.

CD.10FSolution

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.

CD.11CSolution

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.

CD.12TSolution

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#

TasksWhat the error may indicateRepair path
CD.01–02State updates, bindings, aliasingC01; trace before execution
CD.03Contract and boundary interpretationC02; half-open search interval
CD.04Lifetime versus pointer valueC03; draw ownership and scope
CD.05–06Implication, converse, quantifier orderC04; construct countermodels
CD.07Recurrence disconnected from operationsC05 and C07; count each level
CD.08–09Abstract operations versus representationC06; trace actual contents
CD.10Representation and domain limitsC09; decode bits explicitly
CD.11Testing confused with concurrent correctnessC10; identify synchronization
CD.12Transaction and retry boundariesC11–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.01
CD.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.

Back to CD.02
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.03
CD.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.04
CD.05Show solution

Affirming the consequent fails

Set P false and Q true. The implication P⇒\RightarrowQ 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 Q⇒\RightarrowP, which was not supplied.

Back to CD.05
CD.06Show solution

Negate from the outside inward

The first claim is ∀x ∃y V(x,y)\forall x\,\exists y\,V(x,y). Its negation is ∃x ∀y ¬V(x,y)\exists x\,\forall y\,\neg V(x,y): some input has no valid output. The second claim is ∃y ∀x V(x,y)\exists y\,\forall x\,V(x,y), requiring one common output for all inputs. A choice that depends on the input is not necessarily a single common choice.

Back to CD.06
CD.07Show solution

Count a complete recursion level

A model is T(n)=2T(n/2)+cnT(n)=2T(n/2)+cn with a constant-size base case. Each level’s subproblem sizes sum to nn, so its scanning work is cncn. There are log⁡2n\log_2 n internal levels for powers of two, giving Θ(nlog⁡n)\Theta(n\log n) 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.07
CD.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.08
CD.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.09
CD.10Show solution

The representation changes the interpretation

Eight one-bits represent 255 unsigned and 255−256=−1255-256=-1 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.10
CD.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.11
CD.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