You are on page 1of 8

Sasken Confidential

1. Questions ...................................................................................................... 2 1.1. Single dimensional arrays, structures.....................................................................2 1.2. Multidimensional Arrays....................................................................................4 1.3. Algorithms.................................................................................................... 5 1.4. Lists 5 1.5. Strings and Recursion.......................................................................................6 1.6. Queues, Stacks, Hashes and Trees........................................................................6

Sasken Confidential

1. Questions
1.1. Single dimensional arrays, structures
1. [KMAP3] Given an array of positive integers, write a function to check if the array elements can be re-arranged to form an arithmetic series. For example, if the array given contains { 4, 10, 8, 6, 2}, it can be re-arranged as {2, 4, 6, 8, 10}, which forms an arithmetic series. The function should return 1, if it is possible, return 0 otherwise. The function should not modify the input array. The function should work for any size input. The prototype of the function is o int check_arith_series(unsigned int *array, int no_elems). 2. [KMAP3] Given a string that contains C style comments, write logic to check whether the comments are properly formed. Nested comments are allowed. Code a linear time Algorithm. The prototype of the function is int check_comments(char *str) 3. [KMAP 2] You are given a stick of N cms length. You have to break the stick into multiple parts in such a way that, each parts length is a power of 2 and there is only one piece with a given length. Please write a function to list of the length of the various pieces that you would make, given the length N. Do not use % and / operators and power function to do this. The prototype is : void split_stick(unsigned long N) The outputs should be, for N = 10, Ans : 8 2 For N = 11 Ans: 8 2 1 4. [KMAP 2] Write a function to calculate the number of 1s in a binary representation of a given integer. ). Do not use recursion, arrays, power function and division to do this The prototype is : Int count_1s(unsigned long N)

5. [KMAP2] Given a fully filled Sudoku puzzle, develop a program to test if a row of the square is well formed. (int check_sudoko_line(int *array), return 1 if well formed, 0 otherwise). Code a linear time Algorithm. 6. [KMAP2] Given the above function(check_sudoko_square), how will you check if the 9*9 sudoco solution is well formed? You need to repeatedly use the above function on all rows, columns and 3*3 squares. 7. [KMAP2] Write a function to perform case insensitive comparison of 2 strings. The prototype of the function should same as strcasecmp in the C library. 8. [KMAP 2] Write your own implementation of C library function strcat. 9. [KMAP 2] Implement a function to reverse the passed number. For example, if the number passed in 123, return value should be 321. Do this without using arrays. (Using only integer variables).

10. [KMAp2] Implement the atof C library function.

Sasken Confidential

11. [KMAP3] Given the following definition for a student record, write a function to pack an instance of this structure into a byte array in such a way that the byte array can be send over the net to a program running on another machine.. [ You need to address the different representations of structures in different machines, endianess, and pointers). typedef struct student_rec { int rollno; char grade; /* a-f*/ char *name; } student_rec; Unsigned char *send_student(student_rec *rec) Please note that you can not test this code. Write compilable code. 12. [KMAP2] Write a function to perform deep copy of in instance of the above structure passed to it. Student_rec *copy_student(student_rec *rec) Please note the the result pointer should hold the values, even if the original pointer is deleted. 13. Write a function to determine the endianness of your machine. The prototype of the function is : int determine_endian() returns 1 for big endian, 0 for little endian. 14. Write a function to print a inverted triangular pattern, given the number of rows. For example, if the input number is 4, print : * * * * * * * * * * 15. Write a function void togglebit(int *value, int pos) which toggles a single bit in a given position. 16. Write a function int getbits(int value, int start_bitpos, int end_) to return the bits in the range start_bitpos to end_bitpos. Do not use any loops to do this. 17. Write a function void setbits(int *target, int start_bitpos, int end_bitpos, int src) to set the bits in the range start_bitpos to end_bitpos in the target to the lsbs for src. Do not use any loops to do this. 18. [KMAP2] Write function to set the higher 2 bytes of an integer to the mirrored values of its lower 2 bytes? (Mirror all the bits). For example if the input value is : 11111111 00000000 10101111 00001111, if be set as 11110000 11110101 10101111 00001111.

Sasken Confidential

1.2. Multidimensional Arrays


19. [KMAP4] What data structures would you choose to represent a sparse matrix. Write down the structure definitions and following functions to access the sparse matrix. Void Setvalue(SMatrix *m, int I, int j, int value should) Please note that if Setvalue is repeatedly used with the same I and j values, it should only retain the latest value.). If the same index is set multiple times, only the last value should be stored. Int Getvalue(SMatrix *m, int I, int j)

20. [KMAP4] Write a function to allocate a 2D array of integers, in such a way that the array elements can be accessed with a[i][j[ syntax. int **alloc_array( int nrows, int ncols) 21. [KMAP4] Write a function to allocate a 2D array of integers, in such a way that the array elements can be accessed with a[i][j[ syntax. The function should perform only a single malloc call. int **alloc_array( int nrows, int ncols) 22. [KMAP4] Write a function that transposes a given integer matrix and returns the result as per the following prototype? Int **transpose(int **matrix, int nrows, int ncols); 23. [KMAP4] Write a function that transposes a given integer matrix in-place with out using a temporary matrix. The matrix is a square matrix. 24. [KMAP4] Write a function that multiplies the given integer matrices and returns the result as per the following prototype? Int **mat_mul(int **mat1, int **mat2, int nrow1, int ncol1, int nrow2, int ncol2) 25. A video image 10X10 is represented by a 2-dimensional byte array. Develop a program to do run length encoding of the picture into a linear array. Encoding rules are : Non-zero values will be reproduced as is. A string of zeroes will be coded as two integers 0 followed by number of zeroes.

Sasken Confidential

1.3. Algorithms
26. [KMAP4] Write a function that does bubble sort of an integer array. What would be the function prototype that you would propose for a sort function. sort(int array[], int size) many ways to code. 27. [KMAP4] Use the std library function qsort to sort an array of strings and array of integers. 28. Write a function to check if two integer arrays passed to it contain the same elements. The arrays are not sorted. The prototype of the function is : int check_same(int *array1, *array2, int no_elems). Both the arrays are of the the same size. Think about the most efficient way to do this. 29. [KMAP4] Write a function to implement a binary search on an array of integers with the following prototype. Int bin_search(int *array, int no_elems, int value), returns the index, if found, returns -1 otherwise. 30. [KMAP4] Implement a function to subtract 2 very long integers represented as strings. The prototype of the function is char *sub_ap(char *op1, char *op2). The strings op1 and op2 contain the integers represented as a base 10 number and are null terminated. For example, if the op1 contains 12345, the integer equivalent is 12345. [ Do not use the atoi function as the input integers are very big and would not fit in long int).

1.4. Lists
31. Write a function to split a linked list of integers into 2. The first list should contain only odd numbers and second list should contain only even numbers. You should re-use the memory allocated to the nodes in the original list to form the split lists (i.e. your function should not do any memory allocation). After the complete split, original list should be empty. The prototype of the function is : Void splitList(Node **org_list, Node **odd, Node **even) 32. [KMAP3] Write a function Node *Reverse(Node *head) to reverse a singly linked list. 33. [KMAP3] Write a function to find whether there is a cycle in a singly linked list? 34. [KMAP3] Write a function to find a mid point of a singly linked list in a single traversal of the list. Signature of the function is : Node *find_mid(Node *head) 35. [KMAP3] Write a function to insert a node into doubly linked list of integers after a given node. 36. [KMAP3] Write a function to delete a given node in a doubly linked list.

Sasken Confidential

1.5. Strings and Recursion


37. [KMAP4] How many substrings are there for a string of length N. Write a program to print all the substrings of a given string? (If the string is abc, the substrings as a, b, c, ab, bc, abc) 38. [KMAP4] Write a function that reverses a string (Reverse(char *str, ...)) in place. There is a recursive and a non-recursive solution possible.Write both an compare the 2 solutions. 39. Write a function to check if a given string a substring of another. The prototype of the function is : int check_substr(char *large_str, char *substr). Do not use the C library functions. 40. [KMAP3] Write a program to check if a given string is a palindrome. 41. [KMAP4] Write a recursive function to solve the tower of hanoi problem. How many moves are needed for a problem with n disks? (You need to print the moves) 42. [KMAP4] You have a modified version of the tower of hanoi problem, where there is only 1 tower. The tower has disks of various sizes. You have to get them in sorted order. The only operation allowed is to pull out a set of discs, reverse the set of discs and put it back. 43. [KMAP3] Given a set of 3 of points(as (x,y) pairs), write code to determine if they form a triangle. The input is a 2D array with 2 columns and 3 rows, each row represents a point. int Check_for triangle(int **array)

1.6. Queues, Stacks, Hashes and Trees


44. Implement a stack data structure and use it print the contents of a file in reverse order. 45. [KMAP3] Implement 2 queues of integers on a single array. You are give the following. int queue[MAX_STACK_SIZE) enque1(int value); enque2 (int value); int dequeue1(); int dequeue2 (); 46. [kmap3] Implement a circular buffer, for integers using an array as storage. Implement the appropriate enqueue, dequeue functions. 47. [KMAP3] What are hash maps ? When do you use them? Study hash maps with Judy Library. Write a function to read in a set of integers from a file and insert into a hash map. Iterate through the hash map and print the elements in sorted order.

Sasken Confidential

48. Construct the tree for the arithmetic expression : ((1+(2+(3*4))) + (6/2)) and Using the above tree, write code that will generate inorder, post order and pre order traversals of the tree. 49. Given a binary tree, write a function to check if the tree is balanced. 50. Implement a function to make a mirror copy of the passed binary tree. (In the copied tree, left and right children should be swapped).

Sasken Confidential

You might also like