Find two-sum indices and a key-sorted frequency count of an integer array

Quick Overview

Given an integer array and a target, return the indices of the two values that add up to the target together with a count of every value, keyed in ascending order. It tests one-pass hash map lookups, frequency counting and building a deterministic sorted result.

Find two-sum indices and a key-sorted frequency count of an integer array

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

Given an array of integers `nums` and an integer `target`, perform the following two operations using hash maps: - Find the indices of the two numbers in `nums` that add up to `target`. You may assume that each input has exactly one solution, and you may not use the same element twice. - Build a dictionary mapping each unique element in `nums` to its frequency (number of occurrences). The dictionary should be sorted by key. Return a list containing two elements: the two-sum result as a list of two indices, and the frequency dictionary. ### Function Signature ```python def two_sum_and_count(nums: list[int], target: int) -> list: ``` ### Rules - The return value is `[pair, freq]`. - `pair` is a list of two different indices `[i, j]` with `nums[i] + nums[j] == target`, written in ascending order (`i < j`). - `freq` is a dictionary from each distinct value of `nums` (an integer) to its count, with keys in ascending numeric order. In JSON form the keys appear as strings, for example `{"2": 1}`. ### Constraints - `2 <= nums.length <= 10^4` - `-10^9 <= nums[i] <= 10^9` - `-10^9 <= target <= 10^9` - Only one valid answer exists for the two-sum part. ### Examples ```text Input: nums = [2,7,11,15], target = 9 Output: [[0,1],{2:1,7:1,11:1,15:1}] ``` `nums[0] + nums[1] = 2 + 7 = 9`, so the indices are `[0,1]`. Each element appears once. ```text Input: nums = [3,3,4,4,5], target = 6 Output: [[0,1],{3:2,4:2,5:1}] ``` The two 3s at indices 0 and 1 sum to 6, and no other pair does. In the counts, 3 and 4 appear twice and 5 appears once. ```text Input: nums = [-1,5,-1,8,-3], target = 5 Output: [[3,4],{-3:1,-1:2,5:1,8:1}] ``` Only `8 + (-3)` reaches 5, at indices 3 and 4. The keys are in numeric order, so -3 comes before -1.

Overview: Given an integer array and a target, return the indices of the two values that add up to the target together with a count of every value, keyed in ascending order. It tests one-pass hash map lookups, frequency counting and building a deterministic sorted result.

Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

Given an array of integers nums and an integer target, perform the following two operations using hash maps:

  • Find the indices of the two numbers in nums that add up to target . You may assume that each input has exactly one solution, and you may not use the same element twice.
  • Build a dictionary mapping each unique element in nums to its frequency (number of occurrences). The dictionary should be sorted by key.

Return a list containing two elements: the two-sum result as a list of two indices, and the frequency dictionary.

Function Signature

def two_sum_and_count(nums: list[int], target: int) -> list:

Rules

  • The return value is [pair, freq] .
  • pair is a list of two different indices [i, j] with nums[i] + nums[j] == target , written in ascending order ( i < j ).
  • freq is a dictionary from each distinct value of nums (an integer) to its count, with keys in ascending numeric order. In JSON form the keys appear as strings, for example {"2": 1} .

Constraints

  • 2 <= nums.length <= 10^4
  • -10^9 <= nums[i] <= 10^9
  • -10^9 <= target <= 10^9
  • Only one valid answer exists for the two-sum part.

Examples

Input: nums = [2,7,11,15], target = 9
Output: [[0,1],{2:1,7:1,11:1,15:1}]

nums[0] + nums[1] = 2 + 7 = 9, so the indices are [0,1]. Each element appears once.

Input: nums = [3,3,4,4,5], target = 6
Output: [[0,1],{3:2,4:2,5:1}]

The two 3s at indices 0 and 1 sum to 6, and no other pair does. In the counts, 3 and 4 appear twice and 5 appears once.

Input: nums = [-1,5,-1,8,-3], target = 5
Output: [[3,4],{-3:1,-1:2,5:1,8:1}]

Only 8 + (-3) reaches 5, at indices 3 and 4. The keys are in numeric order, so -3 comes before -1.