Buffer Overflow

Rewrite the program shown below so that it is no longer vulnerable to a buffer overflow.

int main (int argc, char *argv[]) {
    bool valid = false;
    char str1[8];
    char str2[8];

/* This is some function that assigns some string to str1 */

/* Read a string from standard input device (eg. keyboard) */

    if ( strncmp(str1, str2, 8) == 0 )

        valid = true;
    printf (“buffer1: str1(%s), str2(%s), valid(%d)n”, 
                                          str1, str2, valid);
Find Solution here

Number of ordered Pairs

Let S be a set of n consecutive natural numbers.
How to find the number of ordered pairs (A,B), where A and B are subsets of S and A is a proper subset of B?

For Example:
Let the set S = {1, 2, 3, 4, 5, 6, 7, 8}.
Find the number of ordered pairs (A, B), where A and B are subsets of S and A is a proper subset of B.

Source: www.testfunda.com

Find Solution here

Subtle Bug

The variable d is less than the Total elements, but still the if condition did not evaluate to true. Find the Bug.

#include <stdio.h>
int array_one[]={1,2,3,4,5,6};
#define TOTAL_ELEMENTS (sizeof(array_one)/sizeof(array_one[0]))

int main()
  int d=-1;
    printf(“d is less than Total no. of elemnts in the array”); //execution never comes her
    printf(“d is greater than total elements in the array”); //this will get printed
  return 0;

Find Answer here

Chess Board Puzzle

You’ve got an 8×8 chess Board and a bunch of dominoes that each fit nicely on two squares of the chess Board.  You can easily tile the entire checkerboard with these dominoes. Now say that you remove two squares, one at one corner and the other at the opposite corner. You’re left with 62 squares.
Can you tile this with the dominoes?
If so, show how. If not, prove why not.

Source: www.careercup.com

Find Answer here

Predict the other’s coin

Assume the following 3-player game consisting of several rounds. Players A and B build a team, they have one fair coin each, and may initially talk to each other. Before starting the first round, however, no more communication between them is allowed until the end of the game. (Imagine they are separated in different places without any communication infrastructure.) A round of the game consists of the following steps:

(1) the team gives one dollar to player C.
(2) Both A and B toss their coins independently.
(3) Both A and B try to predict the other’s coin by telling the guess to C. (No communication: A does not know the outcome of B’s coin toss, and vice versa, nor the guess).
(4) If C verifies that both A and B guess the other’s coin correctly, then C has to give 3 dollars back to the team.
Should C play this game?

Source: Raphael M. Reischuk

Find Solution here

Number of paths

For a rectangular grid having a x b cells as shown in the below figure:-

Find the number of shortest paths along the edge of the cells. Starting from the bottom-left point and end at right-up point.
For simplicity they are marked as (0,0) and (a,b)
Write a pseudo code to print all such

Find Solution here

Candy Game

A group of students are sitting in a circle with the teacher in the center. They all have an even number of candies (not necessarily equal). When the teacher blows a whistle, each student passes half his candies to the student on his left. Then the students who have an odd number of candies obtain an extra candy from the teacher.

Show that after a finite number of whistles, all students have the same number of candies.

Source: www.puzzletweeter.com

Find Solution here