Professional Documents
Culture Documents
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).
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
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
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