All practice tasks

Coding (Algo)

Count Matching Pairs

Hash map · Easy

A reconciliation tool receives integer adjustments in a batch. It must count pairs of different entries whose amounts add to a requested total. Repeated amounts are separate entries and can produce several distinct pairs.

Return the number of index pairs i < j for which nums[i] + nums[j] equals target.

Input

Call

  • Implement solve(input) and return the result. The provided adapter handles JSON input and output.

Input shape

A JSON object { "nums": number[], "target": number }.

Fields

  • nums contains signed integer amounts. target is a signed integer required total.

Output

Return value

  • Return one integer count. The same index cannot be used twice.
  • Equal-valued entries at different indexes remain distinct choices.
  • Inputs of length zero or one return 0.

Constraints

  • At most 100000 values, each an integer in [-1000000000,1000000000].
  • target is an integer in [-2000000000,2000000000]. The count can exceed 32 bits but fits in Number.

Examples

Example 1

{
  "nums": [
    1,
    1,
    1
  ],
  "target": 2
}

Returns

3

Choose any two of the three indexes.

Example 2

{
  "nums": [
    2
  ],
  "target": 4
}

Returns

0

An entry cannot pair with itself.

Required complexity

O(n) expected time and O(n) extra space

What this task practices

Count Matching Pairs is a coding (algo) interview exercise that trains prompt interpretation, explicit assumptions, a concrete response, and a clear explanation of tradeoffs. The catalog marks it as easy difficulty. It focuses on Technical Reasoning, Testing, Validation. The signed-in workspace provides the tools for the round and evaluates the attempt against task-specific criteria. Reference solutions, hidden checks, evaluator instructions, and candidate work remain private.

Sign in to startThe workspace and evaluation open after sign in.