Showing posts with label Interview Question. Show all posts
Showing posts with label Interview Question. Show all posts

Wednesday, April 16, 2014

Python Developer Interview Questions: Fizz-Buzz

Fizz-buzz is a very basic interview question mainly asked to see if a developer/programmer can actually code or not. Nothing too sophisticated.I was asked to code a FizzBuzz today for the second time in my life and I thought why not to blog about it, since it seems to be getting popular to ask from developers. To make it a bit challenging for myself, I coded it in Python, since I have the least skill in coding Python, although I think it is a sexy language.

Interview Question: Write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”."

Solution:
__author__ = 'amir'

for x in range(1, 100):
    if x % 15 == 0:
        print 'fizzbuzz'
    elif x % 3 == 0:
        print 'fizz'
    elif x % 5 == 0:
        print 'buzz'
    else:
        print x

Remark: Just remember that a number that is both divisible by three and five is also divisible by 15. Another note is that you need to check if the divisible by 15 condition first of all, if not they end up in divisible by 3 and 5 condition. Makes sense, right? :)

Sunday, June 23, 2013

.NET Developer Interview Question: What is IDisposable?

Garbage collector as a part of CLR has the responsibility of managing memory allocation to native .NET objects which are called managed objects. C#, VB.NET and other .NET languages which are garbage collected languages does not provide a way for developers to finalize a managed object in their application directly. However not all resources you use in your program is a managed one. Examples of unmanaged objects are window handles, open files, streams and database connections. While CLR's garbage collector automatically releases the memory allocated to managed objects (when they are not needed any longer), it will not be possible to know when garbage collection will occur with unmanaged objects and developers need to take the matter into their own hands. 

When calling a class that implements the IDisposable interface, use the try/finally pattern to make sure that unmanaged resources are disposed, even if an exception interrupts your application. you can use the using statement (Using in Visual Basic) instead of the try/finally pattern.

IDisposable interface is defined as follows:
public interface IDisposable
{
   void Dispose();
}

When writing a class for IDisposable interface, make the dispose method of the class public so that other classes can call the methods:
public class MyClass : IDisposable
{
   public void Dispose()
   {
     //Handle your finalization process here ...
   }
}

Wednesday, June 19, 2013

.NET Developer Interview Question: What is Deferred Execution?

According to MSDN definition, "Deferred execution means that the evaluation of an expression is delayed until its realized value is actually required". The main benefit of using deferred execution is that it can greatly improve performance when you have to manipulate large data collections, especially in programs that contain a series of chained queries or manipulations.


In .NET and Microsoft technologies, C# is supporting deferred execution via using yield keyword (LINQ extensively uses deferred execution as one way of executing its queries).


C# Example:
public class Program
{
    static void Main()
    {
       foreach (int value in ComputePower(2, 30))
       {
          Console.Write(value);
       }
    }

    public static IEnumerable ComputePower(int number, int exponent)
    {
       int exponentNum = 0;
       int numberResult = 1;
  
       // Continue loop until the exponent count is reached.
       while (exponentNum < exponent)
       {
          numberResult *= number;
          exponentNum++;
     
          yield return numberResult;
       }
    }
}

In the above example, the computation of the 2 power 30 will be deffered to where the for-each looped value is used, which will boost up the performance of this chunk of code.


 LINQ Example:
class Program
{
    static void Main(string[] args)
    {
        string[] stringArray = { "abc", "def", "ghi" };

        var q = from str in stringArray.ConvertCollectionToUpperCase()
                select str;

        foreach (string str in q)
            Console.WriteLine("Main: str {0}", str);
    }
    
    public static IEnumerable 
                   ConvertCollectionToUpperCase(this IEnumerable source)
    {
        foreach (string str in source)
        {
            Console.WriteLine("ToUpper: source {0}", str);
            yield return str.ToUpper();
        }
    }
}

In the above example, the same happens with the LINQ queries. Once you use the value of str in the for-each loop, result of ConvertCollectionToUpperCase will be executed. 

Thursday, February 14, 2013

.NET Developer Interview Question: Managed vs Unmanaged Code

Question: What is the difference between managed or unmanaged code? What would you prefer?

Answer: According to Wikipeida, Managed code is a term coined by Microsoft to identify computer program source code, that requires and will only execute under the management of a Common Language Runtime Virtual Machine (resulting in bytecode).

On the other hand, Unmanaged code is what you use to make before Visual Studio .NET 2002 was released. Visual Basic 6, Visual C++ 6, heck, even that 15-year old C compiler. In this scenario, the application builds into assembly code (unlike managed code which builds into bytecode). 

About the preference, it depends on the use cases. When developing for hardware or OS, you have to write unmanaged code or at least, inside a component of your managed code project (when for example working with unmanaged code libraries such as Win32). Whereas when developing a Business Information System, you should avoid the unmanaged code parts as much as possible.

Remark 1: There is a managed version of C++ which was introduced by Microsoft. So you should not get into the trick that C++ applications are categorized unmanaged ones.

Remark 2: Some people use "Native Code" as a synonym for "Unmanaged Code". But you should consider that Native Code might also refer to the output of JIT compiler, the machine code that actually runs inside the environment. 


Thursday, August 30, 2012

Interview Question: Determine Whether a Text includes all ASCII characters

import java.util.Arrays;

import java.util.Scanner;

import java.io.UnsupportedEncodingException;





public class ASCII {



 /**

  * @param args

  * @throws UnsupportedEncodingException 

  */

 static String text = "";

 static boolean[] counter = new boolean[256];



 public static void main(String[] args) throws UnsupportedEncodingException {



  while (text == "") { 

   System.out.println("Enter Your Text and Press ENTER:");

   text = readText();

  }



  byte[] result = text.getBytes("US-ASCII");

  long[] intResult = new long[result.length];

  long value = 0;

  //Converting Byte Array to its Integer equivalence

  for (int i = 0; i &lt; result.length; i++)

  {

     value = (value &lt;&lt; 8) + (result[i] &amp; 0xff);

     intResult[i]= value;

     value=0;

  }

  long[] sortResult = sortArray(intResult);



  if (hasAllASCIIChars(sortResult))

   System.out.println("Text has all ASCII characters");

  else

   System.out.println("Text does not have all ASCII characters");

 }



 



 /**

  * 

  * @param sortedArray: sorted array of ASCII Codes of characters (Long)

  * @return

  */

 private static boolean hasAllASCIIChars(long[] sortedArray) {



  for (int i = 0; i&lt; sortedArray.length - 1; i++) 

   if (!counter[(int)sortedArray[i]])

    counter[(int)sortedArray[i]] = false;



  for (boolean bool : counter)

   if (bool == false)

    return false;

  return true;  

 }



 public static String readText() {

  Scanner sc = new  Scanner(System.in);

  if (sc.hasNext()) {

   return sc.nextLine();

  }

  else {

   return "";

  }

 }



 



 public static long[] sortArray(long[] array) {

  Arrays.sort(array);

  return array;

 }

}