What is Encapsulation

Encapsulation in the mechanism that binds together code and data and that leaps both safe from outside interference or misuse. It also allows the creation of an object. More simply, an object in a logical entity that encapsulate both data and the code that manipulators that data.
Within an object, some of the code and/ or data may be private to the objected and in accessible to anything outside the object. In this way and object provides a significant level of protection against some other unrelated part of the program accidentally modifying or incorrectly using the private parts of the object.
Read more ...

What is Data abstraction in oop

The wrapping up of data and functions into a single unit (called class) is known as encapsulation. Data encapsulation is the most striking feature of a class. The data is not accessible to the outside world and only those functions which are wrapped in the class can access it. These functions provide the interface between the object’s data and the program. This insulation of the data from direct access by the program is called ‘data hiding‘.
Abstraction refers to the act of representing essential features without including the background details or explanations. Classes use the concept of abstraction and are defined as a list of abstract attributes such as size, weight and cost, and functions to operate on these attributes. They encapsulate al1 the essential properties of the objects that are to be created. Since the classes use the concept of data abstraction, they are known as Abstract Data Types (ADT).
Read more ...

Matrices: An introduction

This tutorial discusses some of the ins and outs of matrices. Matrices can be fun, but more importantly, they can really save you time. 
A matrix is, by definition, a rectangular array of numeric or algebraic quantities which are subject to mathematical operations. Matrices can be defined in terms of their dimensions (number of rows and columns). Let us take a look at a matrix with 4 rows and 3 columns (we denote it as a 4×3 matrix and call it A):
Each individual item in a matrix is called a cell, and can be denoted by the particular row and column it resides in. For instance, in matrix A, element a32 can be found where the 3rd row and the 2nd column intersect.
What are they used for?
Matrices are used to represent complicated or time-consuming mathematical operations. A single matrix can hold an infinite number of calculations, which can then be applied to a number, vector, or another matrix. There are several operations that can be done on matrices, including addition, multiplication and inverse calculation; some of which will be discussed shortly. Operations done on one matrix can be transferred to another matrix simply by concatenating the two (by matrix multiplication). Matrices often find their use in 3 dimensional applications, were numerous identical operations are performed on thousands of vectors 30 or 40 times a second. Combining all these operations in one single matrix significantly improves the speed and functionality of a 3D rendering pipeline. Matrices are also used in financial processes (again, where a large number of data has to be processed in a similar fashion).
Operations on Matrices
1.Addition and Subtraction
Addition and subtraction operations can easily be performed on matrices, provided the matrices have the same dimensions. All that is required is to add or subtract the corresponding cells of each matrix involved in the operation. Let us take a look at the addition of two 2×3 matrices, A and B:
2.Scalar Multiplication
Multiplying a matrix by a scalar value involves multiplying every element of the matrix by that value. Here we multiply our 2×3 matrix A by a scalar value β:
3.Matrix Multiplication
The multiplication operation on matrices differs significantly from its real counterpart. One major difference is that multiplication can be performed on matrices with different dimensions. The first restriction is that the first matrix has to have the same amount of columns as the second has rows. The reason for this will become clear shortly. Another thing to note is that matrix multiplication is not commutative i.e., (CD) does not equal (DC).
The procedure for matrix multiplication is rather simple. First, we determine the dimensions of the resultant matrix. All we require is that there are as many columns in the first matrix as there are rows in the second. A simple way of determining is to look at the nearest and farthest dimensions of two matrix symbols written next to each other, for instance: C[2x3D[3x2]. The nearest dimensions are both equal to 3, and so we know that the operation is possible. The farthest dimensions will give us the dimensions of the product matrix, so our result will be a 2×2 matrix. The general rule says that in order to perform the multiplication AB, where A is a (m x n) matrix and B a (k x l) matrix, we must have n=k. The result will be a (m x l) matrix.
Performing the operation product involves multiplying the cells of a particular rows in the first matrix by the cells of a particular column in the second matrix, adding the products, and storing the result in the cell of the resultant matrix whose coordinates correspond to the row of the first matrix and the column of the second matrix. For instance, in AB = C, if we want to find the value of c12, we must multiply the cells of row 1 in the first matrix by the cells of column 2 in the second matrix and sum the results.
4.Matrix/Vector Multiplication
We can also multiply matrices and vectors together, since a vector is nothing more than a 1-column matrix. Consider a 4×4 matrix M which comprises an arbitrary number of transformations (rotation, scaling, translation, etc.) which are to be applied to a 4×1 vector, S. The resultant 4×1 vector R obtained by multiplying M and S together is then a transformation of S.
5.Coding with matrices
How do we define a matrix in C++ you ask? Simple. If a matrix is a two dimensional array of numbers, by definition, then we should be able to define it in C++ that way too. Here’s how:
typedef float MATRIX[4][4]; // A 4×4 matrix
The simplest thing you would want to do is concatenate (multiply) two matrices. We perform the operation, as mentioned earlier, like so:
// matmult: Multiplies matrices A and B, storing the result in A
void matmult(MATRIX &A, MATRIX B)

MATRIX conc;
// Multiplies by rows and columns
for (int i = 0; i <4; i++)
for (int j = 0; j < 4; j++)

conc[i][j] =
(A[0][j] * B[i][0])+
(A[1][j] * B[i][1])+
(A[2][j] * B[i][2])+
(A[3][j] * B[i][3]);

// Copy result matrix into matrix A
for (int p = 0; p < 4; p++)
for (int q = 0; q < 4; q++)
a[p][q] = conc[p][q];
Note: The code references cell yx as cell xy. This may be changed in the near future for the sake of clarity.
You also need to initialize a matrix before you begin to work with it (unless you are simply copying from one matrix to another). Here’s the code to do so:
// matinit: Initializes a matrix, and sets to 'identity'
void matinit(MATRIX &a)

for (int i = 0; i < 4; i++)
for (int j = 0; j < 4; j++)
a[i][j] = 0.0f;
a[0][0] = 1.0f;

a[1][1] = 1.0f;
a[2][2] = 1.0f;
a[3][3] = 1.0f;
Note: The code references cell yx as cell xy. This may be changed in the near future for the sake of clarity.
Matrices are essentially useless unless you apply them to something. In 3D programming, you would want to apply them to vectors. By applying say, a rotation matrix, to a vector, we can rotate that vector around the origin in 3D space. 3D vectors are simply 3×1 matrices. Here is the code to do so:
// vecmatmult: Multiplies vector V and matrix B
void vecmatmult(VECTOR &v, MATRIX b)

float x, y, z, w = 1;
x = (v.x * b[0][0]) + (v.y * b[0][1]) + (v.z * b[0][2]) + (w * b[0][3]);
y = (v.x * b[1][0]) + (v.y * b[1][1]) + (v.z * b[1][2]) + (w * b[1][3]);
z = (v.x * b[2][0]) + (v.y * b[2][1]) + (v.z * b[2][2]) + (w * b[2][3]);

v.x = x;
v.y = y;
v.z = z;
Note: The code references cell yx as cell xy. This may be changed in the near future for the sake of clarity.
What is w you ask? The w is simply to accommodate the translation cells, since the vector is only 3×1 and the matrix is 4×4.
Useful matrices
Here are a couple of useful matrices that you can use. Most of them are for 3D geometry and are 4×4 matrices. I will add some more in the near future and also show how they are derived. Here they are:
The identity matrix is a matrix that does not affect the contents stored in another matrix when multiplied. It can be used as a basis for for constructing other matrices. An identity matrix can be any size, as long as it’s diagonal consists of 1’s and all other cells are filled with zeros, as shown:
The translation matrix is a matrix that can be used to translate vectors, such points, in n-dimensional space. Here, translations are for 3-dimensional space, where tx, ty and tz relate to displacements in the x, y and z dimensions respectively. Each component of the translation is added to it’s respective vector component. Notice how this matrix is based on the identity matrix:
The scaling matrix is a matrix that can be used to scale vectors, such points, in n-dimensional space. Here, scaling values are for 3-dimensional space, where sx, sy and sz relate to scaling factors in the x, y and z dimensions respectively. Notice, once again, how this matrix is based on the identity matrix:
The x-rotation matrix is a matrix that can be used to rotate vectors around the x-axis in 3-dimensional space. Theta (q) is the angle at which the vector is rotated. Lets look at how this matrix is derived:
The process of rotating a vector around the x-axis in 3D space is as follows:
x’ = x
y’ = (y*cosq) + (z*sinq)
z’ = (z*-sinq) - (y*cosq)
When applying this matrix to a vector (by means of a dot product), each of those transformations is only applied once, to the appropriate vector component. Again, these matrices are based on the identity matrix:
The y-rotation matrix is a matrix that can be used to rotate vectors around the y-axis in 3-dimensional space. Theta (q) is the angle at which the vector is rotated. Lets look at how this matrix is derived:
Here, the process of rotation is similar to that of the x-rotation matrix:
x’ = (x*cosq) - (z*sinq)
y’ = y
z’ = (x*sinq) + (z*cosq)
Here is the matrix:
The z-rotation matrix is a matrix that can be used to rotate vectors around the z-axis in 3-dimensional space. Again, theta (q) is the angle at which the vector is rotated. Lets look at a simple derivation:
Here, the process of rotation is similar to that of the x-rotation matrix:
x’ = (x*cosq) + (y*sinq)
y’ = -(x*sinq) + (y*cosq)
z’ = z
Here it is:

Read more ...

Two sticks and matchbox, measure exactly 45 minutes by burning these sticks

Question: You have two sticks and matchbox. Each stick takes exactly an hour to burn from one end to the other. The sticks are not identical and do not burn at a constant rate. As a result, two equal lengths of the stick would not necessarily burn in the same amount of time.  How would you measure exactly 45 minutes by burning these sticks?

Answer: This puzzle used to be asked in Wall Street interviews long time ago. It is very rare for this question to be asked now but it is a very good question to help you think a little outside the normal thought process.

The answer is really simple. Since the sticks do not burn at a constant rate, we can not use the length of the stick as any sort of measurement of time. If we light a stick, it takes 60 minutes to burn completely. What if we light the stick from both sides? It will take exactly half the original time, i.e. 30 minutes to burn completely.

0 minutes – Light stick 1 on both sides and stick 2 on one side.
30 minutes – Stick 1 will be burnt out. Light the other end of stick 2.
45 minutes – Stick 2 will be burnt out.

If you have any interesting questions, feel free to email them to support@goforexam.in
Read more ...

Aptitude Question - what is the proportion of boys to girls in the country?

Question: In a country where everyone wants a boy, each family continues having babies till they have a boy. After some time, what is the proportion of boys to girls in the country? (Assuming probability of having a boy or a girl is the same)

Answer: This is a very simple probability question in a software interview. This question might be a little old to be ever asked again but it is a good warm up.

Assume there are C number of couples so there would be C boys. The number of girls can be calculated by the following method.
Number of girls = 0*(Probability of 0 girls) + 1*(Probability of 1 girl) + 2*(Probability of 2 girls) + …
Number of girls = 0*(C*1/2) + 1*(C*1/2*1/2) + 2*(C*1/2*1/2*1/2) + …
Number of girls = 0 + C/4 + 2*C/8 + 3*C/16 + …
Number of girls = C
(using mathematical formulas; it becomes apparent if you just sum up the first 4-5 terms)

Thus the proportion of boys to girls is 1 : 1.
Read more ...

Finding the sub-array with the largest sum.

Question: You are given an array with integers (both positive and negative) in any random order. Find the sub-array with the largest sum.

Answer: This is an all-time favorite software interview question. The best way to solve this puzzle is to use Kadane’s algorithm which runs in O(n) time. The idea is to keep scanning through the array and calculating the maximum sub-array that ends at every position. The sub-array will either contain a range of numbers if the array has intermixed positive and negative values, or it will contain the least negative value if the array has only negative values. Here’s some code to illustrate.
void maxSumSubArray( int *array, int len, int *start, int *end, int *maxSum )
    int maxSumSoFar = -2147483648;
    int curSum = 0;
    int a = b = s = i = 0;
    for( i = 0; i < len; i++ ) {
        curSum += array[i];
        if ( curSum > maxSumSoFar ) {
            maxSumSoFar = curSum;
            a = s;
            b = i;
        if( curSum < 0 ) {
            curSum = 0;
            s = i + 1;
    *start = a;
    *end = b;
    *maxSum = maxSumSoFar;
Think you have a better solution? We'd love to read your comments!
Read more ...

Like and Share With Your Friends