Showing posts with label basic c interview questions. Show all posts
Showing posts with label basic c interview questions. Show all posts

Time and Space Efficiency

Outstanding developers pay a lot of attention to time and space consumption and have the passion needed to continue improving performance of their own code. When there are multiple solutions for a problem, interviewers always expect the best one. If interviewers point out that there are better solutions, candidates should try their best to find approaches to improving time and space performance, demonstrating his or her enthusiasm to pursue excellence, which is an essential spirit to have as an outstanding developer.
The first thing candidates should understand is how to analyze time and space efficiencies. Various implementations of the same algorithm may result in dramatic performance distinctions, so it is important to analyze performance of an algorithm and its implementation. Take the calculation of the Fibonacci Sequence as an example. The classical solution is based on the recursive equation f(n) = f(n-1) + f(n-2). It is not difficult to find out the time complexity increases exponentially since there are lots of duplicated calculations. However, the complexity will reduce to O(n) if it is calculated iteratively. First of all, f(1) and f(2) are calculated, and then f(3) is based on f(1) and f(2); f(4) is get based on f(2) and f(3). The sequence continues until f(n) is calculated in a loop. Please refer to the section Fibonacci Sequence for more details.
Candidates have to master pros and cons of each data structure and be able to choose the most suitable one to improve performance. For example, it seems that multiple types of data structures are available to get the median of a stream, including arrays, lists, balanced binary trees, and heaps. After analyzing the characteristics of each data type, we find that the best choice is to utilize two heaps—a maximal heap and a minimal heap (section Median in Stream).
Candidates should also be proficient in common algorithms. The most popular algorithms in interviews are about search and sort. It costs O(n) time to scan an array sequentially. However, it is reduced to O(logn) with the binary search algorithm if an array is sorted. The problem “Maximal Number in a Unimodal Array” and “Times of Occurrences in a Sorted Array”are both solved based on the binary search algorithm. The quicksort algorithm is widely used for other problems besides sorting. The Partition function in quicksort can be used to get the kth maximal number out of n numbers and solve the problem “Majority in an Array”and “Minimal k Numbers”.
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

Dynamic Memory Allocation in C Programming

 Dynamic allocation is a unique feature to C. It enables us to create data types and structures of any size and length to suit our programs need within the program. We use dynamic memory allocation concept when we don't know how in advance about memory requirement.
There are following functions to use for dynamic memory manipulation:
  • void *calloc(size_t num elems, size_t elem_size) - Allocate an array and initialise all elements to zero .
  • void free(void *mem address) - Free a block of memory.
  • void *malloc(size_t num bytes) - Allocate a block of memory.
  • void *realloc(void *mem address, size_t newsize) - Reallocate (adjust size) a block of memory.
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community

C Programming Questions and Answers – Pointers and Addresses

Here is a listing of C quiz on “Pointer Addresses” along with answers, explanations and/or solutions:
1. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         char *p = NULL;
  5.         char *q = 0;
  6.         if (p)
  7.             printf(" p ");
  8.         else
  9.             printf("nullp");
  10.         if (q)
  11.             printf("q\n");
  12.         else
  13.             printf(" nullq\n");
  14.     }
a) nullp nullq
b) Depends on the compiler
c) x nullq where x can be p or nullp depending on the value of NULL
d) p q
View Answer
Answer:a
2. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         void *p = &i;
  6.         printf("%d\n", (int)*p);
  7.         return 0;
  8.     }
a) Compile time error
b) Segmentation fault/runtime crash
c) 10
d) Undefined behaviour
View Answer
Answer:a
3. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int i = 10;
  5.         void *p = &i;
  6.         printf("%f\n", *(float*)p);
  7.         return 0;
  8.     }
a) Compile time error
b) Undefined behaviour
c) 10
d) 0.000000
View Answer
Answer:d
4. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int *f();
  3.     int main()
  4.     {
  5.         int *p = f();
  6.         printf("%d\n", *p);
  7.     }
  8.     int *f()
  9.     {
  10.         int *j = (int*)malloc(sizeof(int));
  11.         *j = 10;
  12.         return j;
  13.     }
a) 10
b) Compile time error
c) Segmentation fault/runtime crash since pointer to local variable is returned
d) Undefined behaviour
View Answer
Answer:a
5. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int *f();
  3.     int main()
  4.     {
  5.         int *p = f();
  6.         printf("%d\n", *p);
  7.     }
  8.     int *f()
  9.     {
  10.         int j = 10;
  11.         return &j;
  12.     }
a) 10
b) Compile time error
c) Segmentation fault/runtime crash
d) Undefined behaviour
View Answer
Answer:a
6. Comment on the following pointer declaration?
    int *ptr, p;
a) ptr is a pointer to integer, p is not
b) ptr and p, both are pointers to integer
c) ptr is a pointer to integer, p may or may not be
d) ptr and p both are not pointers to integer
View Answer
Answer:a
7. What is the output of this C code?
  1.     #include <stdio.h>
  2.     int main()
  3.     {
  4.         int *ptr, a = 10;
  5.         ptr = &a;
  6.         *ptr += 1;
  7.         printf("%d,%d/n", *ptr, a);
  8.     }
a) 10,10
b) 10,11
c) 11,10
d) 11,11
View Answer
Answer:d
8. Comment on the following?
    const int *ptr;
a) You cannot change the value pointed by ptr
b) You cannot change the pointer ptr itself
c) Both (a) and (b)
d) You can change the pointer as well as the value pointed by it
View Answer


Answer:a
www.cinterviews.com appreciates your contribution please mail us the questions you have to cinterviews.blogspot.com@gmail.com so that it will be useful to our job search community