30 Day Risk-Free Guarantee:
100% money back if you're unsatisfied.
Book (308 Pages):
  • 150 Programming Interview Questions and Solutions
  • Five Proven Approaches to Solving Tough Algorithm Questions
  • Ten Mistakes Candidates Make -- And How to Avoid Them
  • Steps to Prepare for Behavioral and Technical Questions
  • Interview War Stories: A View from the Interviewer's Side
  • Book Preview and More Info

Video (One Hour):
  • Watch CareerCup's founder perform a totally unscripted technical interview of a candidate.
  • Overview of what interviewers look for in software engineering jobs.
  • Technical questions with white-boarding coding where the candidate does well - and struggles.
  • Interviewer reviews with what went well and poorly.
  • Video Preview and More Info

Resume Review (24 - 48hr)
  • Get your resume reviewed by a trained engineering interviewer. More Info
All Products / Services


Express Prep Package (Book)
$29.99 for e-book | $32.45 for paperback
Buy E-Book Buy on Amazon


Standard Prep Package (E-Book & Video)
Emailed Instantly | $39.99
Buy Standard

Elite Prep Package (E-Book, Video & Resume)
Emailed Instantly | $99.99
Buy Elite
 



Given 1) an int[] array and 2) an int value, how do i find out which two elements in the array add up to the value given? What is the speed of your algorithm? Can it be done faster?

18


Anonymous on November 23, 2008 |Edit | Edit

Hash table

O(n)

rottentomato on November 23, 2008 |Edit | Edit

No hash table
O(nlogn)

Anonymous on November 24, 2008 |Edit | Edit

How?? Can you please explain.

Anonymous on November 24, 2008 |Edit | Edit

sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

Anonymous on November 26, 2008 |Edit | Edit

I love it when people answer specific questions with data structures. Clearly shows they don't know what they are talking about. I have 7 solutions off the top of my head.

1.) Red black tree
2.) AVL tree
3.) Fibonnaci heap
4.) DAG
5.) DFA
6.) Stack
7.) Linear programming

anon on November 27, 2008 |Edit | Edit

I think the point of discussing these questions is to get an idea of how other people would actually implement a solution. Supplying data structures and programming paradigms as answers does no good. It would be nice if you could provide an in depth explanation of the most efficient of your solutions.

T on March 19, 2009 |Edit | Edit

Anonymous wrote:
--------
I love it when people answer specific questions with data structures. Clearly shows they don't know what they are talking about. I have 7 solutions off the top of my head.

1.) Red black tree
2.) AVL tree
3.) Fibonnaci heap
4.) DAG
5.) DFA
6.) Stack
7.) Linear programming

---------------

Idiot. What a silly thing to say. In fact you seem to quite adept at demonstrating your ignorance.

Just saying Hashtable is a perfectly reasonable answer.

Do you understand that people here are of their own will, sharing their time to help others out? So what if they don't include a complete solution, just a few keywords (in this case, hashtable) should be good enough for a determined person to come up with a solution for that.

Anonymous on January 24, 2010 |Edit | Edit

Stack???

Anonymous on November 29, 2008 |Edit | Edit

1) Find the max, using a sort. nlog(n)
2) Populate a bit vector A[] with 1 for each integer in the array, 1 for those not in the array.
3) loop through the integer array, for each int i, if A[n-i]== 1. i and n-i are the two integers that add up to N.

There is no need to search in the 3rd step.

Piyush on December 07, 2008 |Edit | Edit

find min and max in O(n). then define a hash table with hashing function h(num)=num%d, where d=(max-min)/n.
now, for each element in an array search if the hash table contain a value (sum-num[i]) [i.e. search at the index h(sum-num[i])], if yes the present index and the number stored in hash table is the pair. If not, insert into hash table num[i].
Using this function, if we can assume that the numbers are uniformaly distributed, we can say that each bin will have one number on an avg. so the searching will be done in O(1). therefore total complexity is O(n).

Anonymous on January 11, 2009 |Edit | Edit

Good Solution..

T on March 19, 2009 |Edit | Edit

Anonymous wrote...
> Good Solution.

Not really.

Omer on February 07, 2009 |Edit | Edit

could be done in On


static void sumExists(int a[],int sum){
Hashtable ht=new Hashtable();
int tmp=0;
int num1=0,num2=0;
boolean exist=false;

for (int i = 0; i < a.length; i++) {
ht.put(i, i);
}

for (int i = 0; i < a.length; i++) {
num1=a[i];
tmp=sum-num1;
if(ht.get(tmp)!=null){
exist=true;
num2=tmp;
break;
}
}

if(exist){
System.out.println(num1+ "+"+num2+ " = "+sum);
}
else
System.out.println("doest exist");
}

JD on March 06, 2009 |Edit | Edit

Solution:
1. for each element in array add it to hashtable (HashSet in Java)
2. for each element in array, calculate sum-element, now lookup this value in the table. If it exists you're done

Erik on March 22, 2009 |Edit | Edit

sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

This greedy algorithm is quite good.

JD on March 24, 2009 |Edit | Edit

This was my first thought, interviewer didn't like it much since its a nlogn. Shoot for fastest algo possible, which is O(n)

whatsinthename on April 16, 2009 |Edit | Edit

---------
sort the array (nlogn)
i=0, j=n-1 (j is set to last element of the array)

then use following algo ( O(n))

while i<j
if( (arr[i] + arr[j]) > val)
j--;
else if( (arr[i] + arr[j]) < val)
i++;
else
print arr[i], arr[j]

This greedy algorithm is quite good.
-----------

This algo won't work. check it out.

T on April 16, 2009 |Edit | Edit

And why won't it work...?

Add a Text Comment | Add Runnable Code
Name:
Comment:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.








Amazon (1033)Bloomberg LP (403)Qualcomm (117)Adobe (88)Goldman Sachs (78)NetApp (49)IBM (43)Morgan Stanley (33)CapitalIQ (30)Sophos (25)Achieve Internet (23)Electronic Arts (19)Motorola (18)Research In Motion (17)Flipkart (16)
Microsoft (867)Google (141)NVIDIA (98)Yahoo (82)Epic Systems (69)Expedia (47)VMWare Inc (43)Apple (32)Cisco Systems (28)Facebook (23)Infosys (22)Agilent Technologies (19)Sage Software (17)Deshaw Inc (16)FlexTrade (15)
More Companies »
Software Engineer / Dev... (1062)Financial Software Deve... (170)Testing / Quality Assur... (56)Analyst (35)Virus Researcher (25)Field Sales (15)Developer Program Engin... (9)Front-end Software Engi... (6)MyJoB (5)area sales manager (4)Assistant (3)Cabin crew (2)Accountant (1)personnel (1)Intern (1)
Software Engineer in Te... (288)Program Manager (65)Development Support Eng... (47)INTERN(MSIDC) (28)Web Developer (18)System Administrator (10)Consultant (10)Production Engineer (5)Associate Technology L2 (5)AcquireKnowledge (4)Product Security Engine... (3)Solutions Architect (2)Gamer (1)mts (1)Fresh graduate interview (0)
More Job Titles »
Algorithm (1073)Terminology & Trivia (294)C (166)Object Oriented Design (159)Java (121)Testing (114)Arrays (101)Operating System (78)Database (70)Linked List (62)String Manipulation (56)Networking / Web / Inte... (44)Threads (36)Linux Kernel (33)PHP (22)
Coding (511)C++ (204)Behavioral (159)Data Structures (155)Experience (116)Brain Teasers (111)Computer Architecture &... (79)General Questions and C... (73)Trees and Graphs (69)Math & Computation (57)Application / UI Design (45)Ideas (38)System Design (35)Puzzles (30)Bit Manipulation (20)
More Topics »
CareerCup Official Interview Book Daily Questions Requests for Help Mock Interviews Video for Cracking the Coding Interview Job Placement Service CareerCup Blog
My Profile Edit Profile & Email Settings Sign Out