Category Archives: Other comps. IQ

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.

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