Wednesday, June 19, 2013

Interview Question (Programming in C++)

Give the output of the following program :

class Animal
{
  public :
  virtual void draw()
  {
    cout<<”Animal”;
  }
};
class Leopard : public Animal
{
  public :
  virtual void draw()
  {
    cout<<”Leopard”;
  }
};
main()
{
  Leopard l;
  Animal *a=&l;
  l.draw();
}

(A) Leopard (B) Animal (C) LeopardAnimal (D) AnimalLeopard

Answer :
(A) Leopard

Explanation :
The virtual keyword  indicates to the compiler that it should choose the appropriate definition of the function draw not by the type of reference, but by the type of object that the reference refers to.
For more details on the use of virtual keyword : Reference

Written by

Interview Question (Programming)

What does the following function check?

bool f(unsigned int v)
{
  return ((v!=0)&&!(v & v-1));
}

(A) v is an odd number (B) v is a multiple of 2 (C) v is a power of 2 (D) v has a 0 bit

Answer :
(C) v is a power of 2

Explanation :
1) If v is a power of 2,

  • v is never 0 (even 2^0 = 1) and (v != 0) will always be 1(True).
  • (v & v-1) will always be 0(False) and hence their negation will always be 1(True).
2) If v is not a power of 2, either
  • v = 0, or
  • (v & v-1) is something other than 0 and their negation is 0(False)


Written by

Wednesday, June 5, 2013

Cut the Gold Bar Twice and Pay for 7 Days - Puzzle#1

Puzzle : You have a 7 inch gold bar with you. You need to pay the servant 1 inch gold per day of work. (At the end of first day, servant should have 1 inch gold; at the end of second day, he should have 2 inch gold and so on). You may take back some of the given pieces if needed. How will you do this with minimum number of cuts of the gold bar?

Answer : 2 cuts (i.e 3 pieces => a 4 inch piece, a 2 inch piece and a 1 inch piece)

Explanation : TB = Take back



Written by

Saturday, June 1, 2013

Convolution of Two Images (matrix form) - OpenCV - Python

To perform convolution of two matrices


import cv2.cv as cv
import sys

if __name__ == '__main__':
  mat1 = cv.CreateMat(3, 3, 8)
  mat2 = cv.CreateMat(3, 3, 8)
  dst = cv.CreateImage(cv.GetSize(mat1), 8, 3)

  kernel = [[0, 0, 0], [0, 1, 0], [0, 0, 0]]
  matrix2 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
  for i in range(3):
    for j in range(3):
      mat1[i][j] = kernel[i][j]
  for i in range(3):
    for j in range(3):
      mat2[i][j] = matrix2[i][j]

  cv.NamedWindow("convolution", 1)
  cv.Filter2D(mat2, dst, mat1)
  cv.ShowImage('convolution', dst)

  print 'Press any key to quit'
  cv.WaitKey(0)
  print 'Exiting...'
  cv.DestroyAllWindows()
  sys.exit(0)


Written by

Convolution of Two Images - OpenCV - Python

# To convolute two images img1.bmp and img2.bmp

In mathematics and, in particular, functional analysis, convolution is a m-
athematical operation on two functions f and g, producing a third function
that is typically viewed as a modified version of one of the original func-
tions, giving the area overlap between the two functions as a function of 
the amount that one of the original functions is translated.
-Wikipedia


import cv2.cv as cv
import sys

if __name__ == "__main__":
 # Load two images
 im1 = cv.LoadImageM("img1.bmp")
 im2 = cv.LoadImageM("img2.bmp")
 # Create a destination image of the same size that of the source
 dst = cv.CreateImage(cv.GetSize(im1), 8, 3)

 # Create a window named "convolution(1 for colour)"
 cv.NamedWindow("convolution", 1)
 # Convolute the 2 images
 cv.Filter2D(im1, dst, im2)
 # Show the convoluted result
 cv.ShowImage('convolution', dst)

 # Wait for any key press to close the window
 print 'Press any key to quit'
 cv.WaitKey(0)
 # Destroy all created windows and exit
 print 'Exiting...'
 cv.DestroyAllWindows()
 sys.exit(0)


Written by