Programming challenge: find top values in unsorted array

| | No TrackBacks

Recently, I was given a programming problem for a pre-interview job screen.

  • Create a class which implements either the C# or Java interface
  • Implement the two methods defined in the interface. FindMaxValue should return the single highest integer. FindTopNValues should return a list of the highest 'n' values in the source list
  • Any assumptions or design choices made during the implementation should be documented.
C# interface
namespace Test
   public interface IFindTopValues
      int FindMaxValue(int[] values);
      int[] FindTopNValue(int[] values, int count);
Test cases
  • Describe the test cases which you would implement for this class.

Here's the solution I produced. It uses a C# generic PriorityQueue class. Have a look -- it's useful, reusable code.

Update (2008-05-15): I noticed that Julian Bucknall had a change to his priority queue that ordered entries by priority and time of entry. I decided to update my code to provide the same ability.

No TrackBacks

TrackBack URL:


Did you get the job? I've been sent out the same test and wonder if the answer you've got is ok. It looks pretty good to me.


I didn't get the job, actually. And yes, it's a great computer science answer. This solution has the distinct advantage that should you be interested in pulling out more sorted values, you can do so at low cost.

I would not recommend you copy, imitate, or plagiarize this answer. Among other reasons, it would be fairly memorable to anyone who has seen it before.

If I were answering this question again, I'd go for an answer based on LINQ. I suspect that there is more processing in this because LINQ would have to sort the entire array but there are three advantages:
- it's the hot new Microsoft technology and it would show that you are on top of things
- it is dead simple to code and can easily be done in a 1-2 hour time limit
- it is dead simple to test
Chances are, those elements would make the answer desirable to a technical interviewer.

Shame you didn't get the job.

Any LINQ code snippets you could share as I think you might me right about using some new technology?


You can look up LINQ and Take() to see the basic approach.

Leave a comment

Verification (needed to reduce spam):


OpenID accepted here Learn more about OpenID
Powered by Movable Type 4.32-en

About this Entry

This page contains a single entry by Hugh Brown published on May 14, 2008 12:23 PM.

Grammatical pet peeve: the reason is because was the previous entry in this blog.

Weber 87886 Chimney Starter is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.