Weekly Coding Challenge #1: Two Sum

Weekly Coding Challenge #1: Two Sum

Starting today, I'm running a weekly coding challenge here — styled like a newspaper Sunday crossword. Each problem posts on a Sunday, and its solution is revealed the following Sunday, right alongside the next problem. No immediate spoilers, so there's a full week to actually work through it.

Language for the series: Java.

The Problem

Given an array of integers nums and an integer target, return the indices of the two numbers in nums such that they add up to target.

You may assume that each input has exactly one solution, and you may not use the same element twice. You can return the answer in any order.

Constraints

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

Examples

Example 1:

Input: nums = [2,7,11,15], target = 9
Output: [0,1]
Explanation: nums[0] + nums[1] == 9, so we return [0, 1].

Example 2:

Input: nums = [3,2,4], target = 6
Output: [1,2]

No solution this week — that's the point. Think about the brute-force approach first, then ask yourself what you'd need to remember as you scan the array to avoid checking every pair.

The solution to Challenge #1, plus Challenge #2, drops next Sunday.

Why This Pattern Matters

Two Sum looks like a toy problem, but the underlying technique , trade memory for speed by remembering what you've already seen, shows up everywhere outside interviews: hash joins in databases, deduplication in data pipelines, and systems that reconcile offsetting transactions in real time. Once it clicks, you start spotting it everywhere.

No solution this week, that's the point. Think about the brute-force approach first, then ask yourself what you'd need to remember as you scan the array to avoid checking every pair. Please feel free to add your solution proposed on the comments

The solution to Challenge #1, plus Challenge #2, drops next Sunday.

References

LeetCode. (n.d.). 1. Two Sum. LeetCode. https://leetcode.com/problems/two-sum/