Skip to content
DevFlash

Coding test

Coding exercises with answers: worked programming examples

Three new worked exercises: grouping, currency formatting and deduplicating objects by identifier.

  • 5 min read
  • 5 sections
  • Updated November 2, 2026

This guide is under editorial review.

In 1 minute

  • Write inputs and outputs that make the contract verifiable before choosing a data structure.
  • Compare answers after your attempt and explain why each solution meets the rule.
  • Change one requirement, predict its impact and record the case you missed on the first reading.

Coding exercises with answers are useful when you compare decisions rather than the appearance of code. A short solution may implement a different rule from the one you understood. Before reading each answer, write the contract, choose small inputs and predict the output. Then find the first decision where your reasoning differs from the example.

You will solve three independent programming exercises: grouping tasks, formatting Brazilian currency and removing duplicate objects by identifier. Each answer includes an explicit rule and boundaries to test. For hiring formats, start with the coding test guide. Attempt each problem before viewing its implementation, then explain the result in your own words before moving on.

How to study coding exercises with answers

Do

  • Treat the statement as a verifiable agreement. If the rule keeps the first occurrence, write an input that distinguishes that result from keeping the last.
  • Do this before choosing loops, reductions or collection methods. The data structure should serve the rule.

Avoid

  • Treating two solutions as equivalent just because both remove duplicates, even when they keep different occurrences.
  • Letting the data structure silently define behavior.
  • Predict output for an ordinary input, an empty input and an ambiguous one.
  • Implement a first version and execute your cases before consulting the answer.
  • Compare correctness, input mutation, cost and clarity in that order.
  • Repeat the problem with one changed rule to check whether you learned to decide.

When preparing for an online coding test, you can time the first attempt. Initially, keep the review outside that limit. Practice should reveal why an error happened. Finishing quickly without understanding how two answers differ only makes the same mistake easier to repeat in the next session.

Worked exercise: group tasks by status

Receive tasks with an identifier and status, returning open and completed groups. Preserve input order within each group and leave the input array unchanged. Since the contract permits exactly two statuses, an object with two lists makes that explicit. Empty input must produce both empty groups so callers do not need to check whether a key exists.

type Task = { id: string; status: "open" | "done" };
function groupTasks(tasks: readonly Task[]): Record<Task["status"], Task[]> {
const groups: Record<Task["status"], Task[]> = { open: [], done: [] };
for (const task of tasks) {
groups[task.status].push(task);
}
return groups;
}
console.log(groupTasks([
{ id: "a", status: "open" },
{ id: "b", status: "done" },
{ id: "c", status: "open" },
]));

The output keeps a and c in the open group, in that order, and b in done. Each task is visited once, and stored references grow with input size. The lists are new, but task objects remain shared. Mutating a task afterwards is visible through the group. That is different from a deep copy and should be explicit when explaining the solution.

Also test all tasks sharing one status and repeated identifiers. This exercise groups without removing duplicates; do not add that rule unprompted. External data still needs validation before reaching the function. A TypeScript annotation describes expected input but does not inspect a network response at runtime.

Worked exercise: format currency with Intl

Next, accept a safe integer representing cents and return Brazilian currency text. Negative values are allowed because the contract also represents balance adjustments. Reject fractions and unsafe integers. This validation prevents treating 10.5 cents as a valid monetary amount in this exercise. The return value is display text, not a representation for further payment calculations.

const reais = new Intl.NumberFormat("pt-BR", {
style: "currency",
currency: "BRL",
});
function formatCents(cents: number): string {
if (!Number.isSafeInteger(cents)) {
throw new RangeError("Expected safe integer cents");
}
return reais.format(cents / 100);
}
console.log(formatCents(12345));
console.log(formatCents(0));

For 12345, check the representation of 123 reais and 45 cents; for zero, check the currency decimals. Let the formatter determine symbols and separators instead of concatenating them manually. Formatting spaces may differ from ordinary spaces. A screenshot does not establish exact character equality. Also test a negative value and rejection of a fractional cent.

Worked exercise: keep the first object per id

Receive objects with an id and name. When an id appears again, keep the entire first object even if its name changed. Preserve first encounter order. Deduplicating by complete object content or replacing with the latest record would solve different problems. Use a set for accepted identifiers and a list to retain the result.

type Person = { id: string; name: string };
function firstById(people: readonly Person[]): Person[] {
const seen = new Set<string>();
const result: Person[] = [];
for (const person of people) {
if (seen.has(person.id)) continue;
seen.add(person.id);
result.push(person);
}
return result;
}
console.log(firstById([
{ id: "1", name: "Ana" },
{ id: "1", name: "Ana Maria" },
]));

The answer contains Ana because she appeared first. Different ids with equal names must both remain. Also test empty input and a sequence without duplicates. The result contains original references, so do not promise isolated objects. To discuss what this evidence shows in hiring, read the technical assessment criteria.

Comparison and another attempt without memorizing

  1. Change one requirement

    Close the answer and change one requirement at a time. Add status counts to grouping, change the currency locale while keeping the input unit, or reject duplicate ids instead of discarding them.

  2. Predict the impact

    Predict the affected implementation before editing.

  3. Return to the contract

    If that is difficult, return to the contract and trace a small example manually.

Then transfer the method to another context. A React coding challenge adds state and events but still needs clear contracts. For an independent attempt, practice tag normalization. Record which case you missed and why it escaped your first reading instead of saving only a copy of the final solution.

A calendar strip with amber dots spaced progressively farther apart and a rising memory curve above it.
When returning to an exercise, change one requirement and check whether you can explain its effect on the solution.

Frequently asked questions

Where can I find programming exercises with answers?

Start with the three examples in this guide. They provide contracts, solutions and cases to check. Then choose a different problem and apply the process without consulting an answer before your first implementation.

Should I read the answer before attempting the exercise?

Try first, even if you only produce test cases and an incomplete idea. Answers are more useful when they resolve a concrete question. If you need a hint, close the example afterwards and reconstruct the solution while explaining each decision.

How do I know whether my solution is correct?

Compare output with the contract for ordinary, empty and boundary inputs. Check ordering and input mutation too. Passing your examples adds evidence but does not prove that every relevant rule has been covered.

How can I practice without memorizing solutions?

Change a rule, explain its consequences and implement again without copying. Compare alternatives with the same tests. Justifying why a structure satisfies the new contract exercises reasoning beyond syntax.

Practice with real tests

Solve front-end exercises in the browser, with the same tests a reviewer would run.

See the exercises