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
 



We have a stream of incoming numbers (say order#). how will you output the fist n smallest numbers. how will you store them?

ans:-here we will be taking a array of n size and in that we will be storing the first N number's.after that we will be checking every new number in array for wheather any greater t number present in array .if we get we will keep that in the position of the replaced number else we will skip that new number as this won't be in the list of small number's...

14


Anonymous on November 12, 2009 |Edit | Edit

Max heap

saurabhroongta2 on November 13, 2009 |Edit | Edit

I will use a linked list to store the incoming streams rather than a array. Because it need to have a fast insertion and deletion which is not possible with the array.

Augustine on November 15, 2009 |Edit | Edit

Above two are inefficient solutions.

if there are n numbers of which we need to find smallest first k elements, there is a solution to do this in O(n + k(log k)) if the final result should be sorted. if the final result doesn't need to be sorted then it can be done in O(n).

Refer to: http://en.wikipedia.org/wiki/Selection_algorithm

The two solutions below my post are both inefficient. Moreover, suarabhroongta2's solution is wrong and the solution using Max heap reduces to O(n log(k)).

random on November 15, 2009 |Edit | Edit

Why not use a min heap,and carry out the min heap operation k times at any given point in time.

Assuming k << N,where N is the total no. of elements in the heap, the selection step would take approximately k*log(N)time.

Anonymous on November 15, 2009 |Edit | Edit

I dont understand how did u get k* log(N) ...can you please explain. Like for each element from N elements if we insert into heap ...it will take (log k) for each insertion.... wont it?...or I didnt understand your algorithm clearly

Anonymous on December 26, 2009 |Edit | Edit

@Augustine,

You don't have memory for this...selection algorithm.

random on November 15, 2009 |Edit | Edit

@Augustine

Moreover,the selection algorithm provides a solution to the problem of finding the k-th smallest number in a list.

The problem being discussed *here* is different. Read it again.

Thiyanesh on November 16, 2009 |Edit | Edit

@random: When we use a min heap, we don't know when to update the heap or just leave off the incoming number from the stream.
So we need to use a max heap of size "N".
Initially construct a MAX HEAP of the first "N" input numbers from the stream


updateheap(H, num) {
if (H.GetRoot().GetValue() < num) return;
H.DecreaseKey(H.GetRoot(), num);
}

Dumbo on November 16, 2009 |Edit | Edit

Why not store in a binary search tree and output the contents in inorder tree traversal?

Ran on November 19, 2009 |Edit | Edit

We do not know how the input will arrive,so the chances of constructing a balanced binary tree would be concern.I too thgt about using a BST but was not sure if we would have to be concerned about tree being balanced or not.

Anonymous on November 17, 2009 |Edit | Edit

I had the same idea as Dumbo had,if we make use of binary tree to insert elements as they are coming, and later on go with inorder tree traversal.

Anonymous on November 19, 2009 |Edit | Edit

I think the main problem here is with 'storage'. The selection algorithms expect you to have the entire list in memory and accessible. But the heap/array solution works 'on the fly'.

Shil on November 27, 2009 |Edit | Edit

There are several parts of the question.
1) How to store the entire set of data?
Since there is no much requirement stated, we can simly use list or vector and add new item at the end.
2) How to get only the n smallest item?
A max heap or Binary search tree (BST)of size n will do the job. If total number of items is much larger than n it won't be efficient to keep all the items in max heap or BST. In stead keep only n smallest item in max heap or BST and then every time you get a new item you have to replace the max item with new item if it is greater and then rebuild the heap or BST. The time complexity will be log(n) in both cases.
3) Do you need the first n smallest items in sorted order?
Heap won't keep the items sorted whereas BST will do the job.

nilesh on April 07, 2010 |Edit | Edit

maintain a max heap of n number. if coming number is smaller than the maxm number swap the max with the incoming number and then build the heap gain to keep the max.

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