All practice tasks

Coding (Algo)

Selected Cart Total

Hash map · Easy

A cart summary must total only selected products before rendering. Selection events may repeat a product ID.

Return the selected total in integer cents without charging for an ID more than once.

Input

Call

  • Implement solve(input) and return its result. Do not print from solve.

Input object

{ items: {id: string, cents: integer}[], selected: string[] }

Input rules

  • Item IDs are unique; unknown selected IDs are ignored.

What each field means

  • items — available products. Each has a unique id and a price in integer cents.
  • selected — product IDs chosen by the user; the same ID may occur more than once.

Output

Return value and rules

  • Return the total price as one integer number of cents.
  • Count each selected product once. Ignore selected IDs that are not in items.
  • Return 0 when no known product is selected.

Constraints

  • 0 <= items.length, selected.length <= 100000
  • IDs contain 1–40 ASCII characters; 0 <= cents <= 1000000
  • Do not mutate either input array. All totals fit a safe JavaScript integer.

Examples

Example 1

{
  "items": [
    {
      "id": "a",
      "cents": 120
    },
    {
      "id": "b",
      "cents": 250
    }
  ],
  "selected": [
    "b",
    "b",
    "missing"
  ]
}

Returns

250

Duplicate selections count once.

Example 2

{
  "items": [],
  "selected": []
}

Returns

0

Empty cart.

Required complexity

O(n + m) time and O(n + m) auxiliary space

What this task practices

Selected Cart Total 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.