Exercício prático · DSA — The Plan

Practice lab: Solve, vary, and explain an invariant

do caderno de estudos · atualizado em 2 de outubro de 2026
Para quem vai encarar entrevista de DSA2 min

Tente resolver a tarefa antes de consultar os critérios. Compare o resultado e explique o que mudou no seu raciocínio.

Tarefa e critérios de verificação

Task, 20 minutes: given an array of integers and an integer target sum, with all values, sums, and differences in JavaScript's safe-integer range, return two distinct indices whose values add up to the target, or null when no pair exists. If several pairs work, any valid pair is acceptable. Before coding, state a brute-force solution and an invariant for a one-pass map solution.

Check your work:

  • [3, 3] with target 6 returns the two different indices. [3] with target 6 returns null.
  • [-1, 4, 2] with target 3 returns indices 0 and 1. An empty array and a no-solution input return null.
  • Look up the complement among previously seen values before inserting the current value. Explain why this prevents reusing the same index.
  • State expected O(n) time with average constant-time map operations and O(n) extra space. Contrast that with the brute-force pair scan.

Follow-up: solve the sorted-input variant with two pointers and explain the monotonic reason for each pointer move. On a later day, solve a mixed problem without a pattern label. If you need a hint, record it and return to that step instead of counting a copied solution as an independent solve.

No app, a tarefa fica visível e você pode revelar os critérios depois da tentativa. Use as notas para registrar sua solução.

Praticar este exercício no app