Posts

Showing posts from March, 2011

implement atoi function

Write your own C program to implement the atoi() function

nth node from end of a linked list

Write a C program to return the nth node from the end of a linked list.

Copy of a linked list

How to create a copy of a linked list? Write a C program to create a copy of a linked list.

Compare two linked list

How to compare two linked lists? Write a C program to compare two linked lists.

middle of linked list

How will you find the middle of the linked list?

reverse a doubly linked list

Write a program to reverse a doubly linked list.

Find two unique elements

Given an array in which all numbers except two are repeated once. (i.e. we have 2n+2 numbers and n numbers are occurring twice and remaining two have occurred once). Find those two numbers in the most efficient way.

Malloc and Free in c

void * ptr ; size_t size = BUFSIZ ; ptr = malloc ( size ) ; /* some further execution happens here... */ /* now the buffer size needs to be doubled */ size *= 2 ; ptr = realloc ( ptr , size ) ; What is potential error in these set of c statements?

sizeof operator in c

How will you implement your own sizeof() operator in c?

print 1 to n

Write a code in C to print 1 to n without using any loop and inbuilt function.

Buffer OverFlows

Buffer Overflows - Smashing the Stack A buffer overflow occurs when you allow the user to enter more data than your program was expecting, thereby allowing arbitrary modifications to memory. Due to the way the stack is set up, an attacker can write arbitrary code into memory. When functions are called, both the memory to store the variables declared in the function and the memory to store the arguments to the function are pushed onto the stack as part of a "stack frame". Here is a rough picture of what the stack frame would look like for a function call: [memory for variables in the function][FP][ret][function arguments] First, memory is allocated for variables declared in the function. Then the frame pointer, FP, which is used to address variables within the stack frame, then the address to return to after the function call, ret, followed by the arguments to the function. The gist of this attack is that on the stack, for every function call, the ret pointer indicates...

convert a sorted array to binary tree

You are given a sorted Array of numbers in ascending order .Give an efficient algo for converting it in to Height Balanced Binary tree.

inorder traversal of binary tree

Given a binary tree,print its inorder traversal without using recursion and stack.

signed and unsigned integer

In C, int main() { unsigned int a = 10; int b = -19; puts((a+b)>0? "Positive":"Negative"); }    Explain the output.

String and its index

In c: Explain the output #include   int main ()   {     int a = 3 , b = 5 ;     printf (& a [ "Ya!Hello! how is this? %s\n" ], & b [ "junk/super" ]);     printf (& a [ "WHAT%c%c%c  %c%c  %c !\n" ], 1 [ "this" ],         2 [ "beauty" ], 0 [ "tool" ], 0 [ "is" ], 3 [ "sensitive" ], 4 [ "CCCCCC" ]);     return 0 ;   }

classes ,structure and union

What is the difference between classes,structure and union in context of c++ language?

Default constructor in c++

In C++; class CExample { public: int a,b,c; CExample (int n, int m) { a=n; b=m; }; void multiply () { c=a*b; }; }; For the above code what will happen if we declare object of the class as CExample c1? What if we remove the constructor definition and then declare the object?

Waiter and water onto plate

A man in a restaurant asked a waiter for a juice glass, a dinner plate, water, a match, and a lemon wedge. The man poured enough water onto the plate to cover it. "If you can get the water on the plate into this glass without touching or moving this plate, I will give you $100," the man said. "You can use the match and lemon to do this." A few minutes later, the waiter walked away with $100 in his pocket. How did the waiter get the water into the glass?

delete a node in singly linked list whose head is not known

Given a singly linked list whose head is not known ,you have to delete a given node.

mirror of a binary tree

Write a function in c to find the mirror image of a binary tree.Your method should be optimized in memory and time complexity.

Shuffle an integer array

Write a function in c which shuffles the content of an integer array. eg: Input array  {11,22,33,44,55,66,77,88}  output array :  {11,55,22,66,33,77,44,88} please note first and last element should remain as it is.

Compare two numbers

Write a function to compare two numbers and return greater of them without using any comparasion operator and if-else loop.

Palindromes

This year on October 2, 2001, the date in MMDDYYYY format will be a palindrome (same forwards as backwards). 10/02/2001 when was the last date that this occurred on?

inline function

Why is this function defined as inline and not a normal function? __inline static MMI_BOOL mmi_fe_test_display_as_space(U32 ch) { /*----------------------------------------------------------------*/ /* Local Variables */ /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ /* Code Body */ /*----------------------------------------------------------------*/ if (FONT_TEST_CONTROL_CHAR(ch) || FONT_TEST_SPACE_CHAR(ch)) { return MMI_TRUE; } return MMI_FALSE; }

Guess the day

In one small town, there is a liar who lies on six days of the week. But on the seventh day he always tells the truth. He made the following statements on three successive days: Day 1: 'I lie on Monday and Tuesday.' Day 2: 'Today, it's Thursday, Saturday, or Sunday.' Day 3: 'I lie on Wednesday and Friday.' What day does the guy tell the truth?

The Flawed Game

A teacher was having trouble getting her students to learn their facts, but she noticed that her students often played the game "Rock, Paper, Scissors". She made a game with similar rules in order to encourage the students to learn their facts. The class would be divided into three teams: Rock, Paper and Scissors. The teacher would ask the students a question and the class would raise their hands if they knew the answer. If the first person to answer the question correctly is in the Rock team, he or she would choose one player from the Scissors team to be eliminated. Similarly, if a player from the Scissors team was first to answer the question, then he or she would eliminate a member from the Paper team. Lastly, if a player from the Paper team was first to answer correctly, he or she would eliminate a member from the Rock team. The teacher would then continue asking questions until only one team still has members. That team would be declared the winner and all its member...

Microsoft "Murder Mystery"

Inspector Ixolite of the Yard was investigating a murder at Nottonmye Manor. It was a difficult case, and Ixolite was completely stumped until he noticed a message sent to him by the killer cunningly hidden in a newspaper advertisement selling Car Licence Plates. Inspector Ixolite thought about it for a while, and when he had solved the puzzle, immediately arrested the guilty man. Now answer these : Q1) How did Ixolite know the advert was a clue for him? Q2) Solve the code and tell me who Ixolite arrested. Following is the newspaper advert (Car license plates for sale) that Inspector Ixolite saw. Plates For Sale [W 05 NWO] [H 13 HSR ] [O 05 EBM] [D 08 UNE] [U 10 HTY] [N 04 BRE] [N 16 TTE] [I 26 LHC ] [T 10 AEE] [I 26 CNA] [X 22 VDA]

Lawyer and survey agency

A survey agency recently reported their results of a recent survey in the local newspaper. The report clearly states that exactly 100 lawyers were interviewed. Of the one hundred, 75 own BMWs, 95 own Volvos, and 50 lawyers own both a BMW and a Volvo. Within a short time, several lawyers argue that the results are incorrect. How can they tell?

OnMobile Written test puzzle -3

Typical "stars" are drawn in connected, but not repeated, line segments. For example, a 5-point star is drawn as such - line segments AC, CE, EB, BD, DA. The segments must always alternate a constant number of points (in the above case, skipping 1 point in between). Given the information that there is only 1 way to draw a 5-point star, and that there is NO way to draw a 6-point star (in continuous lines, that is), and there are 2 ways to draw a 7-point star, how many different ways are there to draw a 1000-point star?

OnMobile Written test puzzle -2

Three masters of logic wanted to find out who was the wisest one. So they invited the grand master, who took them into a dark room and said: "I will paint each one of you a red or a blue dot on your forehead. When you walk out and you see at least one red point, raise your hands. The one who says what colour is the dot on his own forehead first, wins." Then he painted only red dots on every one. When they went out everybody had their hands up and after a while one of them said: "I have a red dot on my head." How could he be so sure?

OnMobile Written test puzzle -1

In a far away land, it was known that if you drank poison, the only way to save yourself is to drink a stronger poison, which neutralizes the weaker poison. The king that ruled the land wanted to make sure that he possessed the strongest poison in the kingdom, in order to ensure his survival, in any situation. So the king called the kingdom's pharmacist and the kingdom's treasurer, he gave each a week to make the strongest poison. Then, each would drink the other one's poison, then his own, and the one that will survive, will be the one that had the stronger poison. The pharmacist went straight to work, but the treasurer knew he had no chance, for the pharmacist was much more experienced in this field, so instead, he made up a plan to survive and make sure the pharmacist dies. On the last day the pharmacist suddenly realized that the treasurer would know he had no chance, so he must have a plan. After a little thought, the pharmacist realized what the treasurer's plan...