Longest Ascending Sequence

I had a coding interview yesterday, for the first time in a long time, I let my nerves get the better of me, and when presented with an opportunity to code a solution, I wiffed on a simple problem, and frankly, couldn’t leave it alone, so when I got home, I had to solve it.

The request is simple, find the longest sequence of ascending numbers from an array, and return that sequence.  It’s essentially a test to see if you understand iteration, arrays, and can write logic that constructs and compares sequences.  Here’s the solution, I had come up with, (without the corrections outlined below):

Essentially, the code reads the array from left to right, building a new list every time it encounters a value that is less than the previous.  Then, looking at the entire collection of arrays, it’s easy to sort the collection, and grab the first longest sequence.

The Corrections

The gotcha is that the operator is wrong, and the difference in functionality could be easily overlooked.

Consider this sequence: 1,4,6,2,4,0,1,1,9,7

In this case, when using the less than operator, the sequence returned will be 0,1,1,9 as its the longest consecutive sequence of non-decreasing numbers.  Therefore, if we only want the longest consecutive sequence of ascending integers, line 18 is incorrect, and should actually be:

I’ll also note that my return statement could throw an exception because c1 could be null because I’m using FirstOrDefault(), so a simple null check can prevent that error:

Additional reading..

Finally, I did a little more research (google) on the challenge, and there’s a number of articles and publications that outline the problem, including a free ebook “Fundamentals of Computer Programming with C#” by Svetlin Nakov and Veselin Kolev.  see exercise # 5 on page 257 if your interested..

Using PowerShell to Save Time

Recently got a request to produce a report everyday that would be used for supporting a process. The report is only needed for a short time while we add more features. A change request is already underway, but our release cycle is long enough to require a daily task of creating a report.

I wanted a quick automated report that would give me the data I need, this way I wouldn’t be manually executing a query every day of the week.  The solution was a quick PowerShell script that exports the data to a CSV file, set up as a scheduled task.  I imported the SQL from a file in this case because the query is rather ugly..

Its a quick and simple solution, and saves 20-30 minutes a day, and most importantly, it only took 1/2 hour to implement. Since the release is at least two weeks away, I’ve saved myself at least 4 1/2 hours for other things.