Interviews Questions, Algorithms, Aptitude, C Interview Program, C Theory Question, Aptitude Tricks, Test Series,

Showing posts with label Tricky Interview Question. Show all posts
Showing posts with label Tricky Interview Question. Show all posts

Wednesday, 12 June 2019

Tricky Interview#1 Solution

Question:
W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program?

Solution:
//Credits Gaurav and Swathi H I
#include<stdio.h>
#include<stdlib.h>
main(){
char str[10];
scanf("%s",str);
int i=0;
while(str[i]!='\0'){
(str[i]=='a'|str[i]=='e'|str[i]=='i'|str[i]=='o'|str[i]=='u')? printf("%c",str[i]):printf("");

i++;
}
}


Alternative Solution:
//JavaAbhigyan
#include<stdio.h>
#include<stdlib.h>
main()
{
    char str[10],c;
    scanf("%s",str);
    int i=0;
    while((c=str[i])!='\0')
    {
        switch(c)
        {
        case 'a':
        case 'e':
        case 'i':
        case 'o':
        case 'u':
            printf("%c",c);
        }
        i++;
    }

}

Monday, 10 June 2019

Tricky Interview#6

Question:

WAC PROGRAM THAT GIVES THE SIZE OF STRING WITHOUT USING ANY FUNCTION(PRE DEFINED OR USER DEFINED), NO LOOPS AND NO RECURSION.

Solution:

#include<stdio.h>
void main()
{
int a[100],n;
printf("Enter the String\n");
scanf("%s",a);
n=printf("%s\n",a);
printf("%d",n-1);
}

Tricky Interview#5

Question:
How would you write a C program to print 1 to 100 without loop, recursion, or goto?

Solution:

#include<stdio.h>

void hundred() { static int i=1; printf("%d\n",i++); }

void  twenty(){ hundred(),hundred(),hundred(),hundred(),hundred(); }

void four() { twenty(),twenty(),twenty(),twenty(),twenty(); }

int main()
{
    four(),four(),four(),four();

}

Sunday, 2 June 2019

C program to print the truth table for XY+Z


C program to print the truth table for XY+Z  

#include<stdio.h>
#include<conio.h>

void main()
{
int x,y,z;
clrscr(); //to clear the screen
printf(“XtYtZtXY+Z”);

for(x=0;x<=1;++x)
for(y=0;y<=1;++y)
for(z=0;z<=1;++z)
{
if(x*y+z==2)
printf(“nn%dt%dt%dt1”,x,y,z);
else
printf(“nn%dt%dt%dt%d”,x,y,z,x*y+z);
}
}

OUTPUT:


Print “javaabhigyan” with empty main() in C


Write a program that prints “javaabhigyan” with empty main() function.You are not allowed to write anything in main().


1.) One way of doing this is to apply GCC constructor attribute to a function so that it executes before main()

#include <stdio.h> 
  
/* Apply the constructor attribute to myStartupFun()  
   so that it is executed before main() */
void myStartupFun(void) __attribute__((constructor)); 
  
/* implementation of myStartupFun */
void myStartupFun(void) 

    printf("javaabhigyan"); 

  
int main() 



Output:
javaabhigyan


2.) In linux, just override the default definition of _start() function so that it would work as a custom startup code. See this article to understand more.

#include <stdio.h> 
#include <stdlib.h> 
  
int main(void) 


  
// _start() function 
void _start(void) 

    printf("javaabhigyan"); 
  
    // Call main() function 
    int var = main(); 
    exit(var); 


Now compile this by following command

gcc -nostartfiles -o file file.c

Output:
javaabhigyan

Compute average of two numbers without overflow


Given two numbers, a and b. Compute the average of the two numbers.

The well know formula (a + b) / 2 may fail at the following case :
If, a = b = (2^31) – 1; i.e. INT_MAX.
Now, (a+b) will cause overflow and hence formula (a + b) / 2 wont work

Improved Formula that does not cause overflow :

Average = (a / 2) + (b / 2) + (((a % 2) + (b % 2)) / 2)


Below is the implementation :

// C code to compute average of two numbers 
#include <stdio.h>
#define INT_MAX 2147483647
// Function to compute average of two numbers 
int compute_average(int a, int b) 

    return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2); 

  
  int main() 

    // Assigning maximum integer value 
    int a = INT_MAX, b = INT_MAX; 
  
    // Average of two equal numbers is the same number 
    printf("Actual average : %d\n",INT_MAX); 
  
    // Function to get the average of 2 numbers 
    printf("Computed average : %d",compute_average(a, b)); 
  
    return 0; 


Output:
Actual average: 2147483647
Computed average: 2147483647

Implementing ternary operator without any conditional statement


How to implement a ternary operator in C without using conditional statements.

In the following condition : a ? b : c
If a is true, b will be executed.
Otherwise, c will be executed.

We can assume a, b and c as values.


We can code the equation as :
Result = (!!a)*b + (!a)*c
In above equation, is a is true, result will be b.
Otherwise, the result will be c.


// C code to implement ternary operator
#include<stdio.h>

// Function to implement ternary operator without
// conditional statements
int ternaryOperator(int a, int b, int c)
{
    // If a is true, we return (1 * b) + (!1 * c) i.e. b
    // If a is false, we return (!1 * b) + (1 * c) i.e. c
    return ((!!a) * b + (!a) * c);
}

// Driver code
int main()
{
    int a = 0, b = 10, c = 20;

    // Function call to output b or c depending on a
    printf("%d",ternaryOperator(a, b, c));

    return 0;
}

Output:
20

Conditionally assign a value without using conditional and arithmetic operators

Asked in: Google Interview

Given 4 integers a, b, y, and x, where x can only either 0 and 1 only. The ask is as follows:

If 'x' is 0, 
   Assign value 'a' to variable 'y' 
Else (If 'x' is 1)
   Assign value 'b' to variable 'y'.

Note: – You are not allowed to use any conditional operator (including the ternary operator) or any arithmetic operator ( +, -, *, /).

Examples :

Input :  a = 5 , b = 10, x = 1
Output :  y = 10

Input : a = 5, b = 10 , x = 0
Output :  y = 5


An Idea is to simply store both 'a' and  'b' 
in an array at 0th and 1st index respectively.
Then store value to 'y' by taking 'x' as the index.
Below is implementation

// C program  to assign value to y according 
// to value of x 
  
#include<stdio.h> 
  
// Function to assign value to y according 
// to value of x 
int assignValue(int a, int b, int x) 

    int y; 
    int arr[2]; 
  
    // Store both values in an array 
    // value 'a' at 0th index 
    arr[0] = a; 
  
    // Value 'b' at 1th index 
    arr[1] = b; 
  
    // Assign value to 'y' taking 'x' as index 
    y = arr[x]; 
  
    return y; 

  
int main() 

    int a = 5; 
    int b = 10; 
    int x = 0; 
  
printf("Value assigned to 'y' is %d", assignValue(a, b, x); 
    return 0; 


Output :
Value assigned to 'y' is 5

Friday, 3 May 2019

Tricky Interview#3

Question:

Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?



Tricky Interview#3 Solution

Question:

Given only a pointer/reference to a node to be deleted in a singly linked list, how do you delete it?

Solution:

Approach/Algorithm:

A simple solution is to traverse the linked list until you find the node you want to delete. But this solution requires a pointer to the head node which contradicts the problem statement.

The fast solution is to copy the data from the next node to the node to be deleted and delete the next node. 

    // Find next node using next pointer
    struct Node *temp  = node_ptr->next;

    // Copy data of next node to this node
    node_ptr->data  = temp->data;

    // Unlink next node
    node_ptr->next  = temp->next;

    // Delete next node
    free(temp);


C Program:


#include<stdio.h>
#include<assert.h>
#include<stdlib.h>

struct Node
{
    int data;
    struct Node* next;
};

void push(struct Node** head_ref, int new_data)
{
   struct Node* new_node =
             (struct Node*) malloc(sizeof(struct Node));

   new_node->data  = new_data;

   new_node->next = (*head_ref);

   (*head_ref)= new_node;
}

void printList(struct Node *head)
{
   struct Node *temp = head;
   while(temp != NULL)
   {
      printf("%d  ", temp->data);
      temp = temp->next;
   }
}

void deleteNode(struct Node *node_ptr)
{
   struct Node *temp = node_ptr->next;
   node_ptr->data    = temp->data;
   node_ptr->next    = temp->next;
   free(temp);
}

int main()
{
    struct Node* head = NULL;

    /* Use push() to construct below list
    1->12->1->4->1  */
    push(&head, 1);
    push(&head, 4);
    push(&head, 1);
    push(&head, 12);
    push(&head, 1);

    printf("Before deleting \n");
    printList(head);

    deleteNode(head);

    printf("\nAfter deleting \n");
    printList(head);
    return 0;
}

Output:

Before deleting
1 12 1 4 1
After deleting
12 1 4 1

Note:
This solution doesn’t work if the node to be deleted is the last node of the list.


Saturday, 27 April 2019

Tricky Interview#2

Question:

Write a function that moves the last element to the front in a given Singly Linked List.

 For example:

Input Linked List is 1->2->3->4->5.
Output Linked List 5->1->2->3->4.

Solution:

Move last element to front of a given Linked List

Question:
Write a function that moves the last element to the front in a given Singly Linked List.

 For example:

Input: 1->2->3->4->5

Output: 5->1->2->3->4.



Algorithm:

Traverse the list till the last node. Use two pointers: one to store the address of the last node and other for the address of the second last node. After the end of the loop do the following operations.
i) Make second last as last (secLast->next = NULL).
ii) Set next of last as head (last->next = *head_ref).
iii) Make last as head ( *head_ref = last)



 C Program :

#include<stdio.h>
#include<stdlib.h>
struct Node
{
    int data;
    struct Node *next;
};

void moveToFront(struct Node **head_ref)
{
    /* If linked list is empty, or it contains only one node,
      then nothing needs to be done, simply return */
    if (*head_ref == NULL || (*head_ref)->next == NULL)
        return;

    /* Initialize second last and last pointers */
    struct Node *secLast = NULL;
    struct Node *last = *head_ref;

    /*After this loop secLast contains address of second last
    node and last contains address of last node in Linked List */
    while (last->next != NULL)
    {
        secLast = last;
        last = last->next;
    }

    /* Set the next of second last as NULL */
    secLast->next = NULL;

    /* Set next of last as head node */
    last->next = *head_ref;

    /* Change the head pointer to point to last node now */
    *head_ref = last;
}

/* UTILITY FUNCTIONS */
/* Function to add a node at the begining of Linked List */
void push(struct Node** head_ref, int new_data)
{
    /* allocate node */
    struct Node* new_node =
        (struct Node*) malloc(sizeof(struct Node));

    /* put in the data  */
    new_node->data  = new_data;

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref)    = new_node;
}


/* Function to print nodes in a given linked list */
void printList(struct Node *node)
{
    while(node != NULL)
    {
        printf("%d ", node->data);
        node = node->next;
    }
}

int main()
{
    struct Node *start = NULL;

    push(&start, 5);
    push(&start, 4);
    push(&start, 3);
    push(&start, 2);
    push(&start, 1);

    printf("\n Linked list before moving last to the front\n");
    printList(start);

    moveToFront(&start);

    printf("\n Linked list after removing last to the  front\n");
    printList(start);

    return 0;
}


Output:
 Linked list before moving last to the front
1 2 3 4 5
 Linked list after removing last to the front
5 1 2 3 4



Thursday, 11 April 2019

Tricky Interview #1

Question:

W A C Program That Prints All The vowels Given In String Without Using IF and Break Statements In program? 

Submit Ur Answers (Follow The Link)

Link: https://docs.google.com/forms/d/e/1FAIpQLSeBXalNTB5cLX8dC8BlFmzGcItV52GBiUkqaaCM2q_bpaDnKg/viewform?usp=sf_link