Two Sum is the first DSA problem most people solve and the one most interviewers warm up with. We walk through the brute-force O(n²) approach, then the hash-map O(n) solution, with line-by-line Hindi commentary and code in three languages. Then you practise in the in-browser editor.
Neeche is topic ka Hinglish explanation hai, wahi jo hamare course lesson mein audio narration ke saath chalta hai. Code English mein hai, samjhana Hindi mein.
Welcome to Two Sum: yeh past decade ka sabse zyada poocha gaya coding interview question hai. Code likhne se pehle samajh lo iska purpose: yeh ek specific skill test karta hai, kya tum nested loop ko hash map se replace kar sakte ho. Yeh aa gaya toh ek pura problem category unlock ho jaata hai.
Problem statement dhyaan se padho. Tumhe ek integer array aur ek target value diya gaya hai. Tumhe return karna hai do numbers ke indices, numbers nahi, indices, jinki values target ke equal sum hoti hai. Problem guarantee karta hai exactly ek solution exist karta hai, aur same element do baar use nahi kar sakte.
Pehle pattern pehchano, baad mein code likho. Jab bhi tum dekho "do elements jo together ek condition satisfy karte hain": yeh hint hai hashing consider karne ka. Hash map deta hai constant-time lookup, jo letta hai tumhe ek nested loop replace karne: bas ek fast sawaal: "kya maine pehle dekha hai jo mujhe chahiye?"
Brute force se shuru karo taaki samajh aaye tum kya improve kar rahe ho. Do nested loops: har element i ke liye, har baad waale element j ko scan karo, check karo arr[i] plus arr[j] equals target. Yeh O(n squared) hai. Ek lakh element ke array ke liye, dus billion operations. Bahut slow hai.
Ab asli unlock. Jab tum array walk kar rahe ho, pochhne ke bajaay "is number ke liye, iska match kahan hai?": flip karo. Pochho "kya maine pehle wo number dekha hai jo, is mein add karke, target ke equal hota hai?" Wo "pehle dekha" check fast hona chahiye. Hash map iss ko O(1) bana deta hai.
Yeh hai optimal Python code. Bara lines. Dictionary "seen" value se index map karta hai. Hum ek baar loop karte hain. Har index aur value pair ke liye, check karte hain complement seen mein hai kya: agar haan, do indices return karo. Warna current value record karo. Yeh hai pura algorithm.
Teen edge cases jo interviewer test karna pasand karte hain. Pehla: duplicates, agar array hai 3, 3 aur target hai 6? Tumhara code dono occurrences return karna chahiye, same index do baar nahi. Doosra: negative numbers, tumhara hash map negative complements handle karna chahiye. Teesra: no solution, problem guarantee deta hai exist karta hai, par interview follow-up mein gracefully handle karne ko keh sakte hain.
Time complexity O(n) hai. Array ka ek pass, har element par constant kaam. Space complexity O(n): worst case mein, pair milne se pehle lagbhag har element map mein store karte ho. Trade-off brute force ke against: tum n times speedup gain karte ho n extra memory spend karke. Modern interviews mein yeh trade almost always worth hai.
India context: Two Sum ya iska disguised cousin TCS NQT, Infosys SP, Wipro Elite mein aata hai, aur lagbhag har product company online assessment mein Flipkart se le ke Razorpay tak. Agar tum service company technical interview mein ho, Two Sum almost guaranteed expect karo. Agar product company mein ho, harder variation expect karo jo isi skill pe build karta hai.
def two_sum(nums, target):
seen = {}
for i, x in enumerate(nums):
complement = target - x
if complement in seen:
return [seen[complement], i]
seen[x] = i
return []
Complexity: Brute force (two nested loops): time O(n²), space O(1); Hash map (single pass): time O(n), space O(n).
This free course is built for BTech / BE / BSc / MCA students preparing for campus placements at Amazon, Microsoft, Google, Adobe, Flipkart, Razorpay and similar Indian product companies; also professionals targeting switch interviews. If you have ever Googled two sum leetcode solution in hindi (hinglish) with code and bounced off paid-subscription paywalls or content that assumes you already know everything, this is the course for you.
This page is part of our flagship free course, Blind 75 LeetCode. The full course is delivered as short, focused lessons, each with bilingual narration (English and Hinglish), interactive practice widgets, and a clear before-and-after so you can feel yourself getting better. You can pause for weeks and pick up exactly where you left off, your progress is saved.
One phone-number signup unlocks every course on Techwave Academy: AI Bootcamp, Python for Class 4 / 6 / 8 / 10 / CBSE 12, Git, Linux, Spoken English, IOI Prep, Foundation Bootcamp, Blind 75, NeetCode 150, Claude Mastery, Tableau, Power BI, SQL, and Excel. Yes, all 18, forever free.
Blind 75 was assembled by an experienced interviewer specifically to cover every pattern that appears in SDE interviews, no duplicates, no fluff. It is the most efficient list for placement prep.
Both. Each lesson has a Hindi narration track for the explanation and English-only code with comments.
Every solution is provided in all three. Use the one your placement allows. If unsure, start with Python (cleanest), switch to C++ if you target high-frequency-trading or systems roles.
4-6 weeks if you do 2 problems a day with the in-browser editor and re-do tough ones.
Yes, 100% free, like every Techwave course.
Click below to open Blind 75 LeetCode. You will land on the course dashboard, see the week-by-week plan, and can start Lesson 1 immediately. No trial period, no payment screen, no upgrade pop-ups, ever.