Category Archives: Interview Questions

MAXSUM problem

Q.:- find the contiguous sub array within an array which has the largest sum.

for example :- [−2,1,−3,4,−1,2,1,−5,4]
sub array which has the largest sum (6) = [4, -1 ,2 ,1 ]

Solution:- Code snippet :=
IN C lang –

</strong>int max(int x, int y)
{ return (y > x)? y : x; }

int maxSubArray(const int* A, int n1) {
int maxc = A[0], i;
int curr_max = A[0];

for (i = 1; i < n1; i++)
{
curr_max = max(A[i], curr_max+A[i]);
maxc = max(maxc, curr_max);
}
return maxc;

}

IN JAVA :=

<br />class MaxsumSolution {
public:
int maxSubarrat(const vector<int> &A) {
int n=A.size();
int cursum=0, maxSum=-1000000000;

for(int i =0; i<n ;i++) {
curSum = curSum + A[i];
maxSum = max(maxSum, curSum);
if(curSum<0)
curSum = 0;
}
return maxSum;
}
};

Asked in :- Facebook, Paypal, Yahoo, Microsoft, LinkedIn .

Code Puzzle 2 :- Predict the output of this Program :-

<br />int** performOps(int **A, int m, int n, int *len1, int *len2) {
int i, j;
*len1 = m;
*len2 = n;
int **B = (int **)malloc((*len1) * sizeof(int *));
for (i = 0; i < *len1; i++) {
B[i] = (int *)malloc((*len2) * sizeof(int));
}

for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
B[i][n - 1 - j] = A[i][j];
}
}
return B;
}

Let’s say m=3, n=4, and A : [[1,2,3,4], [5,6,7,8], [9,10, 11, 12]]

What would be the output of the following call :

int len1, len2;
int **B = performOps(A, m, n, &len1, &len2);
int i, j;
for (i = 0; i < len1; i++) {
for (j = 0; j < len2; j++) {
printf("%d ", B[i][j]);
}
}

Answer:- Continue reading Code Puzzle 2 :- Predict the output of this Program :-

What is ” Quine ” in programming ?

Answer :-

Quine is basically known as – “A Self-reproducing program”, means a computer program which prints its own coding.This sounds impossible. But it is possible. This is because of some nice properties of programming.it is a consequence of the general so-called “fixed-point” theorem.
A quine takes no input. Quines are named after the American mathematician and logician Willard Van Orman Quine (1908–2000). The interesting thing is you are not allowed to use open and then print file of the program.Quines are actually quite cool. You should learn to write them. They make you think about levels of meaning — about values and their representations.

Here a C code have a look:-

<br />#include <cstdio>
// C-Quine
int main() {
char *s = "#include <cstdio>%c// C-Quine%cint main() {%c char *s = %c%s%c;%c printf(s, 0x0a, 0x0a, 0x0a, 0x22, s, 0x22, 0x0a, 0x0a, 0x0a);%c return 1;%c};";
printf(s, 0x0a, 0x0a, 0x0a, 0x22, s, 0x22, 0x0a, 0x0a, 0x0a);
return 1;
};

Output:-

Quine

The main purpose of interview questions about quine programs is usually to see whether you’ve come across them before. They are almost never useful in any other sense.

Q. How many times the word “Apple” will be displayed on screen ? Before giving answers check the continue and break statements.

<br />#include
using namespace std;
int main()
{
int x;
for(x=-1; x<=10; x++)
{
if(x < 5)
continue;
else
break;
cout<<"Apple";
}
return 0;
}

Answer:- Whenever x is less than 5, the loop will continue then after it will break and nothing will be printed on the screen because it is the property of break statement that it always ends execution of the nearest enclosing do, for or while statement.

What is null pointer?

Answer:-

A null pointer has a reserved value, often but not necessarily the value zero, indicating that it refers to no object. Null pointers are used routinely, particularly in C and C++, to represent exceptional conditions such as the lack of a successor to the last element of a linked list, while maintaining a consistent structure for the list nodes. This use of null pointers can be compared to the use of null values in relational databases and to the “Nothing” value in the “Maybe” monad. In C, each pointer type has its own null value, and sometimes they have different representations.


What is the difference between C and C++ ?

What is the difference between C and C++ ?


Answer:-

                               C                               C++
1. C follows the procedural paradigm 1. C++ is a multi-paradigm language.(Procedural as well as Object Oriented)
2. No virtual Functions are present in C 2. The concept of virtual Functions are used in C++.
3. In C, Polymorphism is not possible. 3. The concept of polymorphism is used in C++.
Polymorphism is the most Important Feature of OOPS.
4. Operator overloading is not possible in C. 4. Operator overloading is one of the greatest Feature of C++.
5. Top down approach is used in Program Design. 5. Bottom up approach adopted in Program Design.
6. No namespace Feature is present in C Language. 6. Namespace Feature is present in C++ for avoiding Name collision.
7. Multiple Declaration of global variables are allowed. 7. Multiple Declaration of global varioables are not allowed.
8. In C

  • scanf() Function used for Input.
  • printf() Function used for output.
8. In C++

  • Cin>> Function used for Input.
  • Cout<< Function used for output.
9. Mapping between Data and Function is difficult and complicated. 9. Mapping between Data and Function can be used using “Objects”
10. In C, we can call main() Function through other Functions 10. In C++, we cannot call main() Function through other functions.
11. C requires all the variables to be defined at the starting of a scope. 11. C++ allows the declaration of variable anywhere in the scope i.e at time of its First use.
12. No inheritance is possible in C. 12. Inheritance is possible in C++
13. In C, malloc() and calloc() Functions are used for Memory Allocation and free() function for memory Deallocating. 13.In C++,  new and delete operators are used for Memory Allocating and Deallocating.
14. It supports built-in and primitive data types. 14. It support both built-in and user define data types.
15. In C, Exception Handling is not present. 15. In C++, Exception Handling is done with Try and Catch block.

 

Anagram Strings :-

NVIDIA Interview Question Software Engineer / Developers

Question:-
write a code to check if the two strings are anagram or not.

Answer:-
An anagram of a string is another string that contains same characters, only the order of characters can be different. For example, “hello” and “hlelo” are anagram of each other.

<br />#include <iostream>
#include <string>
using namespace std;
#define MAX_ASCII 127

int main()
{
char first_str[30],second_str[30];
cout<<"Enter first string:-"<<endl;
cin>>first_str;
cout<<endl<<"Enter second string:-"<<endl;
cin>>second_str;
if ( strlen(first_str) != strlen(second_str) )
{
cout << "Not anagram....!!!" << endl;
return 0;
}
int freq[MAX_ASCII + 1];
for ( int i = 0; i < MAX_ASCII + 1; i++ )
freq[i] = 0;
char* ptr1 = first_str;
while ( *ptr1 != '' ) {
char c = tolower(*ptr1);
freq[(int)c]++;
ptr1++;
}
char *ptr2 = second_str;
while ( *ptr2 != '' ) {
char c = (char)tolower(*ptr2);
freq[(int)c]--;
ptr2++;
}
for ( int i = 0; i < MAX_ASCII + 1; i++ )
if ( freq[i] > 0 )
{
cout <<endl<< "oops...Strings are not anagram....!!" << endl;
return 0;
}
else
{
continue;
}
cout <<endl<< "yeepy...strings are anagram..!!!" << endl;
return 0;
}

Output :-

Untitled

 

Pushing all the zero to the end in an array.

Question (Asked by Amazon ) :- Given a number in an array form, Come up with code to push all the zeros to the end.

Solution :-

#include<iomanip>
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int arr[20];
int len,count=0;
int i=0,n;
cout<<"Enter the size of array:-"<<endl;
cin>>n;
cout<<"enter elements:-"<<endl;
for(int i=0;i<n;i++)
{
cout<<" ";
cin>>arr[i];
}
cout<<"You've Entered :-"<<endl;
for(int i=0;i<n;i++)
{
cout<<arr[i];
}
//--------------sorting------------------
for(int i=0;i<n;i++)
{
if(arr[i] != 0)
arr[count++]=arr[i];
}
while(count<n)
{
arr[count++]=0;
}
cout<<endl<<"Now after pushing all the zeros to the end array is:-"<<endl;
for(int i=0;i<n;i++)
{
cout<<" "<<arr[i];
}
cout<<"nn"<<setw(30)<<"@ myprogworld..!! ";
getch();
return 0;
}

Output :-

Untitled

Brain Teasers IQ

Question :- A man goes to a hardware shop and asks for price of an item. The shop keeper replies that the item is “one for $1″.
The man gives the shop keeper “$3 for 600″. What did the man buy for his newly painted house?

Answer

Put your answer in comment box :-

Brain Teasers answer

Question :- A man goes to a hardware shop and asks for price of an item. The shop keeper replies that the item is “one for $1”.
The man gives the shop keeper “$3 for 600”. What did the man buy for his newly painted house?

Answer:-

Three numbers “6”,”0″ and “0”