Thursday, 28 July 2011

The "FREEDOM" Question



// given a word say "FREEDOM" and another srch string "DOMFREE" find whether the search string can be wrapped to form the first string.

#include <iostream>
#include <String>
using namespace std;

bool isWrappable(string s1, string s2)
{
if(s1.length() != s2.length())
return false;

int index = s2.find(s1[1]);

if(index == -1)
return false;


while(index != -1)
{

string subst1 = s2.substr(index-1);
string subst2 = s2.substr(0,index-1);

string strconsol = subst1 + subst2;

if(s1 == strconsol)
return true;

index = s2.find(s1[1],index+1);
}
return false;
}

int main()
{
string str1("FFREF");
string str2("REFFF");

if(isWrappable(str1,str2))
cout << "Can Wrap";
else
  cout << "Cant wrap";

getchar();
return 0;

}

Interview Questions

Given a file read every word. and give the count of occurances of each word
--- wat is the time and space complexity
==================================

Link list -  2->3->4->5  to make it 2345

==================================

link list  2--3--4--5--6 add

                  1--2--3--4

result:    2--4--6--8--10

==================================


word FREEDOM  sec word DOMFREE
prog to rite if the second can be wrapped to form the first.


=====


    

Tuesday, 26 July 2011

Print a Sqare Matrix in a Helical fashion.

Print a N*N square matrix in a helical way.
to illustrate:



1
 ->
2
 ->
3
 ->
4






 |
5
  ->
6
->
7

8
|



|

|
9

10
 <-
11

12
|





|
13
<-
14
<-
15
<-
16


====================