Wednesday, 1 May 2013

Common Logical Questions Asked in a Technical Interview

Common Logical Questions Asked in a Technical Interview

Well, if you are going for a technical interview in any programming language, in any stream, irrespective of your expertise and experience, you could be asked very simple logical questions which you must know. Interviewer can ask you very basic and famous C programs to check your logic which you had done in very early stage of your life like Fibonacci Series, Factorial Number, Palindrome Number, Prime Numbers etc. You should be able to write down the logic of these basic programs in a minute otherwise it might be very embarrassing for you and will put direct negative impact on the interviewer.

All your domain knowledge, programming language hold could be negated if you get stuck in these problems. So I must advise you to have a quick look on these algorithms before you appear in any technical interview.

Fibonacci Series: To print Fibonacci series up to n numbers

int main()
{
   int n, first = 0, second = 1, next, c;

   printf("Enter the number of terms\n");
   scanf("%d",&n);

   printf("First %d terms of Fibonacci series are :-\n",n);

   for ( c = 0 ; c < n ; c++ )
   {
      if ( c <= 1 )
         next = c;
      else
      {
         next = first + second;
         first = second;
         second = next;
      }
      printf("%d\n",next);
   }
   return 0;
}

Factorial Program: To find out factorial of a number 

int main()
{
  int c, n, fact = 1;

  printf("Enter a number to calculate it's factorial\n");
  scanf("%d", &n);

  for (c = 1; c <= n; c++)
    fact = fact * c;

  printf("Factorial of %d = %d\n", n, fact);

  return 0;
}

Palindrome Number: To check whether a given number is palindrome or not?

int main()
{
   int n, reverse = 0, temp;

   printf("Enter a number to check if it is a palindrome or not\n");
   scanf("%d",&n);

   temp = n;

   while( temp != 0 )
   {
      reverse = reverse * 10;
      reverse = reverse + temp%10;
      temp = temp/10;
   }

   if ( n == reverse )
      printf("%d is a palindrome number.\n", n);
   else
      printf("%d is not a palindrome number.\n", n);

   return 0;
}

Prime Number: To check whether a given number is prime number or not?

int isPrime(int);

int main(){

    int num,prime;

    printf("Enter a positive number: ");
    scanf("%d",&num);

    prime = isPrime(num);

   if(prime==1)
        printf("%d is a prime number",num);
   else
      printf("%d is not a prime number",num);

   return 0;
}

int isPrime(int num){

    int i=2;

    while(i<=num/2){
         if(num%i==0)
             return 0;
         else
             i++;
    }

    return 1;
}

Please do add some more basic programs or algorithms you need to know before going to a technical interview.

5 comments:

  1. yes, this is especially important in Java EE, Spring, etc interview. LOL :)

    ReplyDelete
  2. You have to know math for this because they want optimal algorithm. For Prime number, you need to use sqrt(num) instead of num/2....

    ReplyDelete
  3. You have to know mathematical for this because they want maximum criteria.programs to examine your reasoning which you had done in very beginning on of your lifestyle like Fibonacci Sequence,These are all very beneficial suggestions

    ReplyDelete
  4. Awesome guide! I'm really fortunate to stumble upon this post. I'll have my interview on Tuesday and for the past interviews I've experienced, all those 10 are always being asked! :) Thank you so much! It seems like you've experienced a lot of interviews too already ;)

    ReplyDelete
  5. Here is a prime number algorithm implementation that is in C#. It is more efficient and doesn't fail on invalid input.

    class Program
    {
    static void Main(string[] args)
    {
    uint n;
    string s;

    Console.Write("Enter a positive integer: ");

    if (UInt32.TryParse(s = Console.ReadLine(), out n))
    Console.WriteLine(n + (IsPrime(n) ? " is prime" : " is not prime"));
    else
    Console.WriteLine('"' + s + "\" is not a positive integer");

    Console.ReadKey();
    }

    static bool IsPrime(uint n)
    {
    bool isPrime = true;

    for (int i = 2; isPrime && i <= Math.Sqrt(n); ++i)
    isPrime = n % i != 0;

    return isPrime;
    }
    }

    ReplyDelete