Thursday, 26 May 2011

Programming questions in Technical round3

1.Write an implementation of strlen().

Given a char pointer, strlen() determines the number of chars in a string. The first thing that your strlen() implementation ought to do is to check your boundary conditions. Don't forget the case where the pointer you are given is pointing to an empty string. What about the case where the pointer is equal to NULL? This is a case where you should state your assumptions. In many implementations, the real strlen() doesn't check to see if the pointer is NULL, so passing a NULL pointer to strlen() would result in a segmentation fault. Making it clear to your interviewer that you are aware of both of these boundary conditions shows that you understand the problem and that you have thought about its solution carefully. Example 3 shows the correct solution.

2.Write, efficient code for extracting unique elements from a sorted list of array. e.g. (1, 1, 3, 3, 3, 5, 5, 5, 9, 9, 9, 9) -> (1, 3, 5, 9).

  1. int main()
    {
    int a[10]={1, 2, 4, 4, 7, 8, 9, 14, 14, 20};
    int i;
    for (i = 0;i<9;i++)
    {
    if (a[i] != a[i+1])
    printf("%d\n",a[i]);
    }
    return 0;
    }
    

No comments:

Post a Comment