C Interview Questions
0of 0 voteswrite a generic function API in C so it can any data types like:
insert(a); where a can be integer,char,float
struct generic {
void *data;
struct generic *member
}
0of 0 votesC program to convert little endian to big endian. Implement htons. ?
Also convert little to big endian using UNIONS ?
-1of 1 voteWrite a program to check whether a substring is present in a main string.
0of 0 votesWrite a function:
struct IntList {
int value;
struct IntList * next;
};
that, given a single-linked list L consisting of N integers and a positive integer M, returns the value stored in the M-th element in the list, counting from the end, assuming that the last element in the list has index 1. The function should return −1 if it is not possible to return such a value.
For example, given L such that:
L = 1 -> 2 -> 3 -> 4
and M = 2, the function should return 3, because the second element counting from the end of the list has value 3.
Assume that:
* N is an integer within the range [0..100,000,000];
* M is an integer within the range [0..1,000,000,000];
* each element of L can be any integer from the interval [−2,147,483,648..2,147,483,647].
Complexity:
* expected worst-case time complexity is O(N);
* expected worst-case space complexity is O(1).
0of 0 votesIf an N X N matrix is given, print it in spiral order.
Example: Below is 5 X 5 matrix
i l o v e
d i n t e
n i e e p
a v w r i
m a x e c
Print in spiral order. Output is iloveepicexamandinterview
0of 0 voteswhat will be the output of the following program and why...
#include<conio.h> #include<stdio.h> void main() { int a=10,b=20,diff; diff=&a-&b; printf("Difference=%d",diff); float a1=10.0,b1=20.0; diff=&a1-&b1; printf("\nDifference=%d",diff); char a2='a',b2='z'; diff=&a2-&b2; printf("\nDifference=%d",diff); double a3=10.0,b3=20.0; diff=&a3-&b3; printf("\nDifference=%d",diff); getch(); }
-1of 1 voteIs there any difference between the usage of void pointer and unsigned int ? if yes what the advantage of one over other?
Guys I havn't thought guys commenting here are idiots as well. so please read the detail explanation for it in my below comment. :)
0of 0 votesDesign a Tic Tac Toe Game. Classes Segregation and Code Flow.
0of 0 voteswhite your own printf() function in c ?
0of 0 votesvoid populate(char **s); int main() { char *s; populate(&s); printf("%s", s); // should print "Prasad" free(s); return 0; } void populate(char **str) { // 1. The next two lines is one implementation *str = (char *)malloc(sizeof(char) * 7); strcpy(*str, "Prasad"); // 2. This line seperately is another implementation *str = "Prasad"; }What is wrong, if anything, with the two implementations of populate.
0of 0 votesWhich is best data structure among following for recursion?
Array
stack
queue
linked list
0of 0 votesWrite a code to extract individual blocks from a given matrix....
Eg: if we have a 4x4 matrix you need to extract 2x2 independent matrices and store them in 4 different arrays...
Given matrix:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
For the above matrix the output should be as follows..
Array1: 1 2 5 6
Array2: 3 4 7 8
Array3: 9 10 13 14
Array4: 11 12 15 16
0of 0 votesHow volatile is implemented by compiler? is it just by disabling optimization of the code which uses the variables qualified with volatile or something more than that?
1of 1 voteGiven a very very very large integer(with 1 million digits, say), how would you convert this integer to an ASCII string
0of 0 votesWrite a long running program in C. This program should not hog the CPU, use no sleep()/block()/select()/wait(), should not block resources...and should terminate after a very very very long time
0of 0 votesHow will you tell whether a given number is a palindrome?
Edit: The question was for a string actually.
0of 0 votesHow will you reverse a given number? (not using "/", "&" operators)
For eg 756 - > 657
0of 0 votesGiven a BST, how would you return the nth smallest element. The code had to cover all the edge cases and was expected to write a logn solution
0of 0 votesWAP to compare string without library function in c??
1)Program should be designed using Pointers
2)Most efficient i.e lesser number of loops or comparisons??
0of 0 votesA log file which has user details(user ID,timestamp) and pages visited in a particular day by that user.The next day -the same kind of log file gets generated.How do you find the probability of users who logged in consecutive days out of the second day - logged in users? The question is simple,but they look for the efficient data structure and time complexity.
0of 0 votessuppose you have a string like-I am a king ,
in this string you have to write code to count the total number of words which have only single alphabet .for this string answer sholud be 2.
1of 1 voteGiven a string we have to find first non-repeating character in the string....
Example: str="aabbbccccddeffffgg";
Answer is : e
0of 0 votesWrite a function to check whether the two strings are rotation of each other or not.
Example: str1="Password" str2="ordPassw"
My Solution was.........#include<stdio.h> void fn(char * str1, char * str2) { int i=0, j=0,flag=0; while(str2[i]) { if(str2[i]== *str1) { flag=1; break; } i++; } if(!flag) { printf("2nd String is not a rotation of other\n"); return; } j=i; while(str2[i]) { if( !(str2[i]==*str1)) { printf("2nd String is not a rotation of other\n"); return; } i++; str1++; } i=0; while(i<j) { if( !(str2[i]==*str1)) { printf("2nd String is not a rotation of other\n"); return; } i++; str1++; } if(*str1=='\0') printf("String are rotation of each other\n"); else printf("2nd String is not a rotation of other\n"); } int main() { char * str1="Password"; char * str2="ordPassw"; fn(str2, str1); return 0; }
0of 0 votesconstruct a BST given its preorder traversal. solution which i gave :-
make first element of array as node of tree and then if element is less than root and if greater then on right. but i got the answer right for the given example but i am not sure if it was right. can you please suggest me a method to do it.
0of 0 voteswrite a program with 2 threads. one thread should print even and other should print odd numbers in sequence. how would you make it SMP safe?
0of 0 votesGiven a variable, how can you find whether it was allocated from stack or from heap memory???
0of 0 votesHow to write an XOR cipher program using the
C programming language.
Your program must accept as input from the user a value between 0 and 255 to be used as the
secret key, the name of the input file and the name of the output file. No line in the input file
should contain more than 4096 characters.
After the user would have provided their secret key, your program should read and perform an
XOR cipher on the contents of the input file and write the result to the output file.
If the input file has already been encrypted and the identical secret key that was used to
perform the initial encryption is provided, then the contents of the output file should be
deciphered into its original plain text.
0of 0 votesGiven a word and special characters print all the combinations of that word along with atmost 2 special characters
Word - test
special characters 0-9,%,#,&,!
Eg: test, test1, test2, te3st, te1!st, test#%, 12test, !te5st, t0est&, etc. etc.
0of 0 votesHow to write an XOR cipher program using the C programming language.
Program must accept as input from the user a value between 0 and 255 to be used as the secret key, the name of the input file and the name of the output file. No line in the input file should contain more than 4096 characters.
After the user would have provided their secret key, your program should read and perform an XOR cipher on the contents of the input file and write the result to the output file.
If the input file has already been encrypted and the identical secret key that was used to perform the initial encryption is provided, then the contents of the output file should be deciphered into its original plain text.
Show:
1, Algorithm and flow chart
2, Source code.
Anyone with a clue to this task?
0of 0 votesGiven a big string (str1) find (s1) and replace by (s2).
Example :
str = "Hi i am abc and i am in abc"
s1 = "abc"
s2 = "pqrstuv"
So final o/p :
str1 = "Hi i am pqrstuv in pqrstuv"
