Posts

Tech Qu: Some SQL Server Questions

Image
The Idiot ones I have gathered a few from some interview sites, and the ones below are so trivial that it’s shocking that they were asked!   WriteTSSL to find all the duplicate email address in a table which contains only one column "email" Code Snippet select [email] from [sometable] where [email] is not null OR len ( [email] )= 0 group by [email] having count ( [email] ) > 1   Many answers failed to exclude no email (could be recorded as null or an empty string), or return the count with it (which was not asked for).   Given a table: CustomerOrders: (Customer ID | Order ID | Order Date) 1. Write a SQL to find all customers who has placed an order today. 2. Write a SQL to find all customers who has placed an order today AND yesterday. Again, a trivial one Code Snippet Select CustomerId from CustomerOrders Where OrderDate >= Cast ( GetDate as Date ) Select CustomerId from CustomerOrde...

Tech Qu: Removing duplicates from an integer array

Image
Write two algorithms to remove duplicates from an integer array.   There are many solutions including sorting the array and then finding duplicates. My preference would be the following ones Code Snippet public static int [] NoDuplicate1( int [] arr) {    var map = new HashSet < int >();    foreach ( var i in arr)     map.Add(i);    return map.ToArray(); } public static int [] NoDuplicate2( int [] arr) {    var map = new Queue < int >();    foreach ( var i in arr)      if (!map.Contains(i))       map.Enqueue(i);    return map.ToArray(); }   And testing: Code Snippet int [] a = { 6,5,4,3,1,2,1, 2, 3, 4, 5, 6, 5, 6 };       var test = Questions .NoDuplicate1(a); foreach ( var i in test) {    Console .WriteLine(i); } test = Questions .NoDuplicate2(a); foreach ( var i in test) {    Console .WriteLine(i); }   Both approaches had the sequence maintained of the first integer found in th...

Tech Qu: Intersect two arrays

Write the code to find the intersection of two arrays, a,b.   The code can be very simple (or horribly complex) from the right perspective: Code Snippet public static int [] IntersectionArray( int [] arr, int [] arr2) {    var result = new HashSet < int >();    var map = new HashSet < int >();    foreach ( var i in arr)    //  if (!map.Contains(i))       map.Add(i);    foreach ( var i in arr2)      if (map.Contains(i) ) // && !result.Contains(i))       result.Add(i);    return result.ToArray(); }   I have included some unneeded condition tests as comments above (HashSet does not retain duplicates and it is assumed that an explicit test would be more expensive then letting the hashset do it itself).   Unit testing is trivial Code Snippet int [] a = { 1, 2, 3, 4, 5, 6, 5, 6 }; int [] b = { 3, 4, 3, 4, 3, 4, 3, 4, }; var test = Questions .IntersectionArray(a, b); foreach ( var i in test) {   ...

Random number generator from binary input

Image
This question is likely if you are dealing with quantum randomizers, etc. It’s irrelevant if there is an appropriate function in the language. You are given a function that generates 0 and 1 with equal probability. Write a function that uses the above function to generate any n(1<=n<=1000) so that probability of producing any number between 1 to 1000 is equal and is 1/1000. The solution is simple and fast using bit shifting (which gives performance, *2 could be used but that will raise flags with many interviewers) Code Snippet static Random _Ran = new Random (); public static int ZeroOne() {    return _Ran.Next(2); } /// <summary> /// We bitshift for optimal performance /// </summary> /// <returns></returns> public static int Random1to1000() {    var num = 0;    var outofrange = true ;    while (outofrange)   {     num = ZeroOne() << 1;      for ( int i = 0; i < 8; i++)     {       num = (num |...

The bizarreness of coding questions–it is time to cut the garbage?

Image
There have been a trend in the industry to evolve to pro-forma interviews running off stock questions that are actually rote based and often irrelevant to the position.    For an example that may make sense to you: Asking for any job that require writing emails , the following questions dealing with literacy: What is a Oxford Comma? What is a split infinitive and give me an example of a valid exception to the rule. What is the difference between obtuse and abstruse? What is the collective noun for a group of crows? (murder); quail? (covey); magpies? (tidings), etc If someone fails these questions, people could assert they are low literacy, and thus not qualified. In reality a person that aces these questions may write incomprehensible emails, while someone that funks these questions may write elegant clear emails.   In terms of coding questions, for a C# position you may get asked about single linked lists and how to do an AddBefore or other...

Using Elmah with Azure Table Storage

Image
Article Summary This article will explain how to extend Elmah to log to and view errors from Windows Azure Table Storage.  Introduction Elmah is an exception handling and logging tool that plugs into ASP.NET and ASP.NET MVC applications. When an error is thrown, Elmah grabs all the information including the stack trace, server variables, and query string. Then this information is entered into the data container of your choice. When you want to review these exceptions, the tool provides a web interface to display the errors. Your Windows Azure Usage This article assumes you already have a Windows Azure account and know how to manage data in Azure Table Storage. I use either the Visual Studio Server Explorer or the Azure Storage Explorer ( codeplex ) to look at the tables. A longer list of Storage Viewer applications is referenced at the bottom of this article. Visual Studio Server Explorer Azure Storage Explorer Steps to Connect Elmah to Windows Azure Table Storag...

Tech Qu: Finding the minimum of the maximum difference.

Given 3 arrays A,B,C find three indices i,j,k such that the value of I = max((a-b),(b-c),(c-a)) is minimized. i.e. a = A[i] b = B[j] c = C[k] This is one of those problems that you can accidentally head down a rabbit hole. Remember the KISS principle. Instead of trying to do an elegant solution, write a direct solution and then attempt optimize it if need be.   Code Snippet public static string MinOf3Arrays( int [] a, int [] b, int [] c)     {        var results = "no result" ;        var min = int .MaxValue;        foreach ( var ai in a)          foreach ( var bi in b)            foreach ( var ci in c)           {              var max = Math .Max( Math .Max(ai - bi, bi - ci), ci - ai);              if (max < min)             {               min = max;               results = string .Format( "{3} Min from: A@i={0}, B@j={1}, C@k={2}" , ai, bi, ci, min);                           }           }       ...