# Write a function two_sum(nums, target) using a hash map

Canonical URL: <https://datadriven.io/problems/write-a-function-twosumnums-target-using-a-hash-map-for-6b850c3b>

Domain: Python · Difficulty: medium

## Problem

Write a function two_sum(nums, target) using a hash map. For each number, check if its complement (target - num) has been seen. Given nums = [2, 7, 11, 15] and target = 9, print the indices.

## Related

- [All practice problems](https://datadriven.io/problems)
- [Mock interview mode](https://datadriven.io/interview/write-a-function-twosumnums-target-using-a-hash-map-for-6b850c3b)
- [Python Interview Questions](https://datadriven.io/python-interview-questions)
- [Data Engineering Interview Prep Guide](https://datadriven.io/data-engineer-interview-prep)
- [Daily Challenge](https://datadriven.io/daily)

---

Source: DataDriven (https://datadriven.io). 100% free data engineering interview prep. Live code execution against Postgres 16, Python 3.11, and Spark sandboxes. No paywall, no premium tier, no signup gate.