Solve the Rat In A Maze problem using backtracking.

This is one of the classical problems of computer science. There is a rat trapped in a maze. There are multiple paths in the maze from the starting point to the ending point. There is some cheese at the exit. The rat starts from the entrance of the maze and wants to get to the cheese. This problem can be attacked as follows. Have a m*m matrix which represents the maze. [Read More]

Write a C program which when compiled and run, prints out a message indicating whether the compiler that it is compiled with, allows /* */ comments to nest.

#include int allowed(){ int a=1; /* /* */ a=0; // */ return a; } int main(){ if (allowed()) printf(“Nested comments Allowed\n”); else printf(“Nested comments not Allowed\n”); return 0; } In case of nested comments not allowed, we have it as /*/* */ a=0;// */ Bigger font show commented area…….. What if the single line comment style is not supported? After all, single line style comments were introduced with C++……….. #include [Read More]

Find the nth Ugly Number

Problem Ugly numbers are numbers whose only prime factors are 2, 3 or 5. The sequence 1, 2, 3, 4, 5, 6, 8, 9, 10, 12, 15, … shows the first 11 ugly numbers. …By convention, 1 is included. Write a program to find and print the 1500’th ugly number.   Solution Method 1 - Brute force Loop for all positive integers until ugly number count is smaller than n, if an integer is ugly than increment ugly number count. [Read More]

Given a string s1 and a string s2, write a snippet to say whether s2 is a rotation of s1 using only one call to strstr routine? (eg given s1 = ABCD and s2 = CDAB, return true, given s1 = ABCD, and s2 = ACBD , return false)

**Syntax of strstr** const char * strstr ( const char * str1, const char * str2 ); Returns a pointer to the first occurrence of _str2_ in _str1_, or a null pointer if _str2_ is not part of _str1_. The matching process does not include the terminating null-characters. **Approach1** Append str1 to itself and do a strstr for str2. That should work. (I am assuming strstr looks for string x within string y). [Read More]