Posts

Showing posts from January, 2013

Program to reverse a linked list using recursion

I generally find Complex and confusing code on internet of reversing a linked list.So this is the simplest code of reversing a linked list using recursion. Algorithm :- 1)we will take the head node as first node and rest all nodes as rest node and recursively call the rest node again. 2)when we get the first node and rest node in the last call of recursion.we will make following first_node->next->next=first_node /* first next of next will be pointing to the first node */ first_node->next=NULL /*first next will be null.Thus we have reversed first and rest node */ 3) Finally when all the recursion call is over we will have reversed linked list. Program :- Its just a function of reversing a linked list void Reverselinkedlist (struct node **head) { struct node *first_node; struct node *rest_node; if(*head==NULL) return 0; first_node=*head; rest_node=first_node->next; if(rest_node==NULL) return 0; Reverselinkedlist(&rest_node); first_node->next->next=first_node; first_

Program to uncompress a string ie a2b3c4 to aabbbcccc

Below is the program to uncompress a string #include<stdio.h> #include<conio.h> #include<stdlib.h> int main() { char str[100]="a2b3c4d8u7"; for(int i=0;str[i]!='\0';i++) { if(i%2!=0) { for(int j=0;j<atoi(&str[i]);j++) { printf("%c",str[i-1]); } } } getch(); } Want to become a Data Engineer? Check out below blog posts  1.  5 Key Skills Every Data Engineer needs in 2023 2.  How to prepare for Data Engineering Interviews 3.  Top 25 Data Engineer Questions

Program to check whether two input strings are anagram or not

#include<iostream> using namespace std; #include<conio.h> void anagram(char str[20],char str1[20]) { int l1=0; int l2=0; int j=0; int r=0; int a1=0; int a2=0; int c1=0; int c2=0; int count=0; int c=0;  while(str[j]!='\0')  {  l1++;  j++;  }   while(str1[r]!='\0')  {  l2++;  r++;  }   while(c2<=l1)  {  if(str[c2]==' ')  a1++;  c2++;  }   while(c1<=l2)  {  if(str1[c1]==' ')  a2++;  c1++;  }  if((l1-a1)==(l2-a2))  {                   for(int i=0;i<l1;i++) { for(int j=0;j<l2;j++) { if(str[i]==str1[j]) { for(int k=1;k<l2-j;k++) { if(str[i]==str1[j+k]) continue; } str[i]='*'; count++; } } } if(count-a1==l1-a1 && count-a1==l2-a2) cout<<"It is Anagram\n"; else cout<<"Not an Anagram\n"; } else cout<<"Not an Anagram\n";  } int main() { char s1[20],s2[20]; cout<<"Enter strings\n"; gets(s1); gets(s2); anagram(s1,s2); getch(); } This program has complexity >O

Program to find the 2nd largest number in an array

This is the program i written in my interview.There can be a better program or logic for the same #include<stdio.h> #include<conio.h> int secondlargest(int s[],int k) { int s1[5],index,ind; int max=s[0]; /* Find the max element in array and get its index*/ for(int i=1;i<k;i++) { if(s[i]>max) { max=s[i]; ind=i; } } /*Substract the array elements from the maximum element and store the new elements in new array,make again the max element index position as max element to avoid having zero in array.*/ for(int i=0;i<k;i++) { s1[i]=max-s[i]; s1[ind]=max; } /*Now find the minimum element in the new array and get its index*/ int min=s1[0]; for(int i=1;i<k;i++) { if(s1[i]<min) { min=s1[i]; index=i; } } /*Return the element having index of above step from the old array.You will have second max element of array*/ return s[index]; } int main() { int arr[5]={35,81,49,1,90}; int s=secondlargest(arr,5); printf("%d",s); getch(); }

Einstein's Puzzle

I guess many of you know about this puzzle but still iam posting for the ones who dont know.This is a puzzle made by Sir Albert Einstein once and told that 98% of people cannot solve this Puzzle.Though after trying for 30 minutes i solved it :-) You can try solving it.I will post answer for this after sometime. Puzzle *There are five houses that are each a different color (Red,Yellow,Green,Blue,White) *There is a person of different nationality in each houses(German,British,Danish,Norwegian,Swedish) *The five owners drink a certain drink (Milk,Beer,Water,coffee,Tea),smokes certain cigarettes (Pall mall,Prince,Dunhill,Blend,Bluemaster)and have different pets (Dog,Bird,Horse,cat,Fish).None of them uses same things Some of the specification given of them:- 1)The British man lives in Red House 2)The swedish man has a dog as pet 3)The Danish man drinks Tea 4)The green house is to left of the White House 5)The owner of green house drinks coffee 6)The person who smokes Pall mall has a bird 7)

Programs and Puzzles in technical interviews i faced

Image
I have attended interview of nearly 10 companies in my campus placements and sharing their experiences with you,though i did not got selected in any of the companies but i had great experience facing their interviews and it might help you as well in preparation of interviews.Here are some of the puzzles and programs asked to me in interview in some of the good companies. CHECK-OUT the VIDEO of  Technical Interview for SAP Labs, CA Tech & HP R&D 1) SAP Labs I attended sap lab online test in my college through campus placements.It had 3 sections,the first one is usual aptitude questions which i would say were little tricky to solve.The second section was Programming test in which you were provided snippet of code and you have to complete the code (See Tricky Code Snippets  ).The code are from different data structures like Binary Tree, AVL Tree etc.Then the third section had questions from Database,OS and Networks.After 2-3 hours we got the result and i was shortlisted