🍪 TechCookies
HomeDSASystem DesignJSMy Progress
Free
Log inStart free
TechCookies — Practice · Learn · PrepareTechCookies — Practice · Learn · Prepare
ConceptsPracticeSD challengesPricingPrivacyTermsContact
© 2026 TechCookies
DSA›Big O & Complexity›LC 1 Two Sum
easyExpert mode
LC 1Two Sum
easy⟺ Big O & ComplexityArrayHash Map
Pattern match🔗 Concept powering this problem
Ω
Big O & Complexity
0 of 10 sections complete
↗
📖 Review concept first
🎯 Pattern recognition check
Before coding: identify why big o & complexity works here.
Why algorithm efficiency matters. Time vs space tradeoffs and how to read Big-O notation.
Problem
You have an array of integers and a target sum. Find the two numbers that add up to the target and return their positions (indices). Each input has exactly one valid answer. You cannot reuse the same element at the same index twice. Return the pair of indices in any order.
Examples
Example 1
Input: nums = [2,7,11,15], target = 9
Output: [0,1]
// nums[0] + nums[1] = 2 + 7 = 9
Example 2
Input: nums = [3,2,4], target = 6
Output: [1,2]
Constraints
  • 2 ≤ nums.length ≤ 10⁴
  • -10⁹ ≤ nums[i] ≤ 10⁹
  • Only one valid answer exists
⚡Focus on reducing time complexity from the naive solution.
TypeScript⚡
JavaScript
JavaPro
PythonPro
GoPro
loading compiler…
Loading editor…
🧪 Test cases3 tests · not run
▾
Test 1
nums = [2,7,11,15], target = 9
Expected: [0,1]
Test 2
nums = [3,2,4], target = 6
Expected: [1,2]
Test 3
nums = [3,3], target = 6
Expected: [0,1]
Target complexity for the optimal solution to this problem.
⏱ Time complexity
O(n)
Single pass — look up complement in O(1) via a hash map.
💾 Space complexity
O(n)
Hash map stores at most n value → index pairs.
Difficulty
easy
Concept map
Big O & Complexity↗
Problems
3 problems · big o pattern
LC 1Two SumeasyLC 217Contains DuplicateeasyLC 125Valid Palindromeeasy
Browse all problems →
✓ Accepted · 0:00 · Solutions unlocked