Você está na página 1de 8

1. 2. Write a routine to draw a circle (x ** 2 + y ** 2 = r ** 2) without making use of any floating point computations at all.

The basic idea is to draw one quadrant and replicate it to other four quadrants. Assuming the center is given as (x,y) and radius as r units, then start X from (x+r) down to (x) and start Y from y up to (y+r). In the iteration, keep comparing is the equation is satisfied or not within an error of one unit for x and y. If not then re-adjust X and Y.

3. Given only putchar (no sprintf, itoa, etc.) write a routine putlong that prints out an unsigned long in decimal.
4. void putlong(unsigned long x) 5. { 6. // we know that 32 bits can have 10 digits. 2^32 = 4294967296 7. for (unsigned long y = 1000000000; y > 0; y /= 10) { 8. putchar( (x / y) + '0'); 9. x = x % y; 10. } 11. }

12. Give a one-line C expression to test whether a number is a power of 2. [No loops allowed - it's a simple test.] if (x && !(x & (x-1)) == 0)

13. Give a very good method to count the number of ones in a 32 bit number. (caution: looping through testing each bit is not a solution).

14. What are the different ways to say, the value of x can be either a 0 or a 1. Apparently the if then else solution has a jump when written out in assembly.
if (x == 0) y=0 else y =x

There is a logical, arithmetic and a datastructure soln to the above problem.

15. I was given two lines of assembly code which found the absolute value of a number stored in two's complement form. I had to recognize what the code was doing. Pretty simple if you know some assembly and some fundaes on number representation.

16. Give a fast way to multiply a number by 7. Multiply by 8 (left shift by 3 bits) and then subtract the number.
(x << 3) - x

1. Switch the integer values stored in two registers without using any additional memory. To swap the values, you can carry out the following instructions:
Reg_1 = Reg_1 + Reg_2; Reg_2 = Reg_1 - Reg_2; Reg_1 = Reg_1 - Reg_2;

1. 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).
2. int main() 3. { 4. int a[10]={1, 2, 4, 4, 7, 8, 9, 14, 14, 20}; 5. int i; 6. for (i = 0;i<9;i++) 7. { 8. if (a[i] != a[i+1]) 9. printf("%d\n",a[i]); 10. } 11. return 0; 12. }

1. Write an algorithm to detect loop in a linked list. You are presented with a linked list, which may have a "loop" in it. That is, an element of the linked list may incorrectly point to a previously encountered element, which can cause an infinite loop when traversing the list. Devise an algorithm to detect whether a loop exists in a linked list. How does your answer change if you cannot change the structure of the list elements? One possible answer is to add a flag to each element of the list. You could then traverse the list, starting at the head and tagging each element as you encounter it. If you ever encountered an element that was already tagged, you would know that you had already

visited it and that there existed a loop in the linked list. What if you are not allowed to alter the structure of the elements of the linked list? The following algorithm will find the loop: 1. Start with two pointers ptr1 and ptr2. 2. Set ptr1 and ptr2 to the head of the linked list. 3. Traverse the linked list with ptr1 moving twice as fast as ptr2 (for every two elements that ptr1 advances within the list, advance ptr2 by one element). 4. Stop when ptr1 reaches the end of the list, or when ptr1 = ptr2. 5. If ptr1 and ptr2 are ever equal, then there must be a loop in the linked list. If the linked list has no loops, ptr1 should reach the end of the linked list ahead of ptr2 2. Question: How would you reverse a doubly-linked list? 3. Answer: This problem isn't too hard. You just need to start at the head of the list, and iterate to the end. At each node, swap the values of pNext and pPrev. Finally, set pHead to the last node in the list.
Node * pCurrent = pHead, *pTemp; while (pCurrent) { pTemp = pCurrent->pNext; pCurrent->pNext = pCurrent->pPrev; pCurrent->pPrev = temp; pHead = pCurrent; pCurrent = temp; }

Question: You are given an array with integers (both positive and negative) in any random order. Find the sub-array with the largest sum. Answer: This is an all-time favorite software interview question. The best way to solve this puzzle is to use Kadane s algorithm which runs in O(n) time. The idea is to keep scanning through the array and calculating the maximum sub-array that ends at every position. The sub-array will either contain a range of numbers if the array has intermixed positive and negative values, or it will contain the least negative value if the array has only negative values. Here s some code to illustrate.

void maxSumSubArray( int *array, int len, int *start, int *end, int *maxSum ) { int maxSumSoFar = -2147483648; int curSum = 0; int a = b = s = i = 0; for( i = 0; i < len; i++ ) {

curSum += array[i]; if ( curSum > maxSumSoFar ) { maxSumSoFar = curSum; a = s; b = i; } if( curSum < 0 ) { curSum = 0; s = i + 1; } } *start = a; *end = b; *maxSum = maxSumSoFar; }

Test cases for student registration form


1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. Launch an IE browser and go to the Registration Form. Verify the registration page opens. On the registration page, click the mouse in the First Name field. Leave the First Name and Last Name fields blank and click on the Submit button. Verify an error message appears saying you cannot leave the First Name and Last Name fields blank. Enter 50 chracters in both the First and Last Name fields. Verify the names are accepted. Enter more than 50 chracters in both the First and Last Name fields. Verify an error message appears saying you cannot enter more than 50 characters in the First Name and Last Name fields. Enter numbers in the First and Last Name fields. Verify an error message appears saying you cannot enter numbers in the First and Last Name fields. Enter the chracters "`~!@#$%^&*()_:";'{}[]+<>?,./" in the First and Last Name fields. Verify an error message appears saying you cannot enter "`~!@#$%^&*()_:";'{}[]+<>?,./" characters in the First and Last Name fields. Type John in the First Name field. Click the mouse in the Last Name field. Type in Doe in Last Name field. Click on the Submit button. Click on registration List in the left nav bar. Verify the Name John Doe is now present in the registration list.

Even the above set of test cases takes a few shortcuts by testing validation on both fields at once. A more correct way to do it would be to test the field validation independently.

For example, the blank name validation should be tested like this:

1. 2. 3. 4. 5. 6.

Leave the First Name and Last Name fields blank and click on the Submit button. Verify an error message appears saying you cannot leave the First Name and Last Name fields blank. Enter a valid First Name and leave the Last Name field blank and click on the Submit button. Verify an error message appears saying you cannot leave the Last Name field blank. Enter a valid Last Name and leave the First Name field blank and click on the Submit button. Verify an error message appears saying you cannot leave the First Name field blank.

Test cases for web search engine


Test cases would be based on the requirement documents. Based on the requirements we would be able to create the functional test cases, User Interface test cases, Messages etc. For eg. the types of test cases would be 1) Is the main textbox where we enter the search data is working correctly, as in can it take alphabets, numbers etc 2) Are the buttons and the textlinks that are displayed working correctly 3) Once you type anything in the search textbox,how long does it take to refresh the page and come up with a result 4) Once you type in the search textbox,how many search results is it bringing up based on the data entered 5) Is it going correclty to the next page,if the functionality is supposed to do that. Then based on the above list we can do different types of testings like Boundary Value analysis or Equivalence partitioning etc.

Searching a string in a set of strings


Given a string, search it in a set of strings (say among 1000s of string). What data structure would you use to store those 1000 strings and get the results fastest? Solution 1 : Simple, but not best solution Create a hashtable, and store all the strings in that hashtable. For the given string, search in that hashtable. In the worst case, if we get same hashcode for all the strings, then we have to compare with all the strings. Solution 2 : Best solution Create a Trie structure, and search for the string in that trie. Depending upon how we construct the trie, the complexity varies from O(n) to O(n*m) where n is the no.of characters in the given string, and m is the no.of different characters that we have in all the strings. In the trie, for each node, if we create children for all the possible characters, then the complexity will be O(n), where n is the no.of characters in the given string. The memory requirement will be high in this case. If we don't create children for all the possible characters, and create children only for the strings that exist in our set, then at each level, we have to search among the children to find the string. So, the complexity would be O(n*m) where n is the no.of characters in the given string, and m is the no.of different characters that we have in all the strings. In this case, we will not use extra memory.

Write a routine that takes as input a string such as "aabbccdef" and outputs "a2b2c2def" or "a4bd2g4" for "aaaabddgggg". Write a ship quality code.
+HUHLVDVHFXUHDQGUREXVWFRGH "

1 2 3 4 5 6 7

#include < math.h >

// function that finds number of digits in the // repeated count of the character int numberOfDigits(int num) { int digits=0;

while(num!=0)
9

{
10 11 12 13 14 15} 16 17 18 19 20

num/=10; digits++; } return digits;

void replaceDuplicateByCount(char *str) { //return if the string is null if(*str=='\0')

21

return;
22 23 24 25 26 27

int len=strlen(str); int j=0; //i and j are the indices used as pointers

28 29 30 31 32 33

for(int i=0;i < len;i++) { if(str[j]=='\0') { str[i]='\0'; break;

34

}
35 36 37 38 39 40 41 42 43 44 45 46

//replace i by j str[i]=str[j]; int count=0;

// find out the repeated count if the // character while(str[j]==str[j+1]) { j++; count++;

47

}
48 49 50 51 52 53 54 55 56 57

// if the character appears 4 times then we // get count as 3 because it is repeated that many times if(count!=0) { i++; //increment count to actual character occurrence count++; //find number of digits int digits=numberOfDigits(count);

58 59 60 61 62 63

// if there is only 1 digit then we need go through // the loop, here we extract the first digit and // store it in the string while(digits!=1) {

64

int divisor=(int)pow((double)10,digits);
65 66 67 68 69 70 71 72

int num = count/divisor; count=count%divisor; str[i]=(char)(num+48); i++; digits--; } str[i]=(char)(count+48); }

j++; } }

Você também pode gostar