Follow me on Linkedin

Showing posts with label C++. Show all posts
Showing posts with label C++. Show all posts

Given a string find the length of longest substring which has none of its character repeated? for eg: i/p string: abcabcbb length of longest substring with no repeating charcters: 3 (abc)

Given a string find the length of longest substring which has none of its character repeated?
for eg:
i/p string:
abcabcbb
length of longest substring with no repeating charcters: 3 (abc)

Program :

#include<stdlib.h>
#include<stdio.h>
#define NO_OF_CHARS 256

int min(int a, int b);

int longestUniqueSubsttr(char *str)
{
    int n = strlen(str);
    int cur_len = 1;  // To store the lenght of current substring
    int max_len = 1;  // To store the result
    int prev_index;  // To store the previous index
    int i;
    int *visited = (int *)malloc(sizeof(int)*NO_OF_CHARS);

    /* Initialize the visited array as -1, -1 is used to indicate that
       character has not been visited yet. */
    for (i = 0; i < NO_OF_CHARS;  i++)
        visited[i] = -1;

    /* Mark first character as visited by storing the index of first
       character in visited array. */
    visited[str[0]] = 0;

    /* Start from the second character. First character is already processed
       (cur_len and max_len are initialized as 1, and visited[str[0]] is set */
    for (i = 1; i < n; i++)
    {
        prev_index =  visited[str[i]];

        /* If the currentt character is not present in the already processed
           substring or it is not part of the current NRCS, then do cur_len++ */
        if (prev_index == -1 || i - cur_len > prev_index)
            cur_len++;

        /* If the current character is present in currently considered NRCS,
           then update NRCS to start from the next character of previous instance. */
        else
        {
            /* Also, when we are changing the NRCS, we should also check whether
              length of the previous NRCS was greater than max_len or not.*/
            if (cur_len > max_len)
                max_len = cur_len;

            cur_len = i - prev_index;
        }

        visited[str[i]] = i; // update the index of current character
    }

    // Compare the length of last NRCS with max_len and update max_len if needed
    if (cur_len > max_len)
        max_len = cur_len;


    free(visited); // free memory allocated for visited

    return max_len;
}

/* A utility function to get the minimum of two integers */
int min(int a, int b)
{
    return (a>b)?b:a;
}

/* Driver program to test above function */
int main()
{
    char str[] = "ABDEFGABEF";
    printf("The input string is %s \n", str);
    int len =  longestUniqueSubsttr(str);
    printf("The length of the longest non-repeating character substring is %d", len);

    getchar();
    return 0;
}


Tags : Amazon Interview Papers , Amazon Placement Papers , Amazon Interview Question , Amazon Interview C Questions , Amazon Interview , Amazon Interview Papers , Amazon Placement Papers , Amazon Interview Question , Amazon Interview C Questions , Amazon Interview , Google Interview Papers , Google Placement Papers , Google Interview Question , Google Interview C Questions , Google Interview , Google Interview Papers , Google Placement Papers , Google Interview Question , Google Interview C Questions , Google Interview , Microsoft Interview Papers , Microsoft Placement Papers , Microsoft Interview Question , Microsoft Interview C Questions , Microsoft Interview , Microsoft Interview Papers , Microsoft Placement Papers , Microsoft Interview Question , Microsoft Interview C Questions , Microsoft Interview , TCS Interview Papers , TCS Placement Papers , TCS Interview Question , TCS Interview C Questions , TCS Interview , TCS Interview Papers , TCS Placement Papers , TCS Interview Question , TCS Interview C Questions , TCS Interview , Wipro Interview Papers , Wipro Placement Papers , Wipro Interview Question , Wipro Interview C Questions , Wipro Interview , Wipro Interview Papers , Wipro Placement Papers , Wipro Interview Question , Wipro Interview C Questions , Wipro Interview , HCL Interview Papers , HCL Placement Papers , HCL Interview Question , HCL Interview C Questions , HCL Interview , HCL Interview Papers , HCL Placement Papers , HCL Interview Question , HCL Interview C Questions , HCL Interview , Zoho Interview Papers , Zoho Placement Papers , Zoho Interview Question , Zoho Interview C Questions , Zoho Interview , Zoho Interview Papers , Zoho Placement Papers , Zoho Interview Question , Zoho Interview C Questions , Zoho Interview , Accenture Interview Papers , Accenture Placement Papers , Accenture Interview Question , Accenture Interview C Questions , Accenture Interview , Accenture Interview Papers , Accenture Placement Papers , Accenture Interview Question , Accenture Interview C Questions , Accenture Interview , Infosys Interview Papers , Infosys Placement Papers , Infosys Interview Question , Infosys Interview C Questions , Infosys Interview , Infosys Interview Papers , Infosys Placement Papers , Infosys Interview Question , Infosys Interview C Questions , Infosys Interview , Facebook Interview Papers , Facebook Placement Papers , Facebook Interview Question , Facebook Interview C Questions , Facebook Interview , Facebook Interview Papers , Facebook Placement Papers , Facebook Interview Question , Facebook Interview C Questions , Facebook Interview ,  Interview Papers ,  Placement Papers ,  Interview Question ,  Interview C Questions ,  Interview ,  Interview Papers ,  Placement Papers ,  Interview Question ,  Interview C Questions ,  Interview


Tricky C Questions



1) what will be the output of the following Printf function.

printf("%d",printf("%d",printf("%d",printf("%s","ILOVECPROGRAM"))));

Ans-ILOVECPROGRAM1321

The above printf line gives output like this because printf returns the number of character successfully written in the output.So the inner printf("%s","ILOVECPROGRAM") writes 13 characters to the output so the outer printf function will print 13 and as 13 is of 2 characters so the next outer printf function will print 2 and then next outer printf will print 1 as 2 is one character.So is the output 1321.

2) What will be the output of the following conditional operator.

a=0?(3>2?23:(2>5?(7<6?34:48):64)):1
printf("%d",a);

Ans-1

The above code snippet is very simple actually but it is made to look like that it is very tough and most of us start solving the nested part without thinking a bit although we know the concept.So first think for five minutes.The concept behind this is when we use conditional operator then if the condition is true then we select the value before the colon and if it is false then the value after the column.
For example a>b?a:b.If a is greater then b then a otherwise b.So we have 0 before ? operator that means false so no need to see the nested thing.The answer will be 1.

3) if(condition)
printf("I love" );
else
printf("C Language");

What should be the condition inside if statement such that it will print "I Love C Language" ?

Ans- if(!printf("I Love"))

As the printf returns the number of characters successfully written on output it will return 6.And making it not will invert and make it 0 so else statement will print out.

4) Program to identify even or odd number without using any arithmetic operator,conditional statement,logical operators,Relational operator.This program was asked in Microsoft written test.

Ans- int main()
{
scanf("%d",&no);
(no&1)?printf("odd"):printf("even");
}

The above question can be solved using bitwise AND operator as it is not mentioned in question not to use.And also you can use conditional operator as conditional statement cannot be used.Taking bitwise AND of any number with 1 will give value y where y=0 if number is Even and y=1 if number is Odd because even number always ends with 0 so 0001 & ---0 will always give 0.On the other hand odd number always have 1 in last position so 0001 & ---1 will always give 1.

5) Program to find the sum of the digits of a number in single statement.

Ans- int sum(int x)
{
int s;
for(s=0;x>0;s+=x%10,x/=10);
return s;
}

6) Print number from 1-100 without using loop,Recursion and Goto.

Ans - #include <stdio.h>
#include<conio.h>
#define STEP1 step();
#define STEP2 STEP1 STEP1
#define STEP4 STEP2 STEP2
#define STEP8 STEP4 STEP4
#define STEP16 STEP8 STEP8
#define STEP32 STEP16 STEP16
#define STEP64 STEP32 STEP32
#define STEP128 STEP64 STEP64

int n = 0;

int step()
{
if (++n <= 100)
printf("%d\n", n);
}

int main()
{
STEP128;
getch();
}

7) Deallocate memory without using free() in C

Ans- void *realloc(void *ptr,size_t size);
if size=0 then call to realloc is equivalent to free(ptr)

As realloc is used to deallocate previously allocated memory and if the size=0 then it will acts as free().


Tags : Tricky C Question , Tricky C++ Question , Tricky Interview Question , Interview Question with answers , Easy interview question with answers



Push all the zero's of a given array to the end of the array. In place only. Ex 1,2,0,4,0,0,8 becomes 1,2,4,8,0,0,0


#include<stdio.h>
#include<conio.h>
#define size 11
using namespace std;
int main()
{
    int arr[size] = {1,9,9,4,0,0,2,7,0,6,7}; 
    int current;    
    for(int j=0; j<size-1;j++)
    { 
      if(arr[j]==0)
      {
      current=j;
      break;
      }}                  
    int pos = current;  
    while( current <size )
     {   
 if( arr[current] != 0 && current != pos )
       {   
 arr[pos] = arr[current];    
 ++pos;    
 }   
 ++current;
 }  
   while( pos<size)
     {
 arr[pos] = 0;
 ++pos;
 }
    for(int i=0;i<size;i++)
    printf("%d",arr[i]);    
 getch();
 return 0;     
}
                             

How many times “Hello World” is printed by following program? int main() { if(fork() && fork()) { fork(); } if(fork() || fork()) { fork(); } printf(“Hello world”); return 0; } a. 16 b. 20 c. 24 d. 64

How many times “Hello World” is printed by following program?
int main()
{
if(fork() && fork())
{
fork();
}
if(fork() || fork())
{
fork();
}

printf(“Hello world”);
return 0;
}

a. 16
b. 20
c. 24
d. 64


Solution : 24


a