Adap.tv Interview Question
Developer Program EngineersCountry: India
Interview Type: In-Person
At first glance I thought this was a simple problem.... then I tried to code it. Took me a little mental gymnastics, but I figured it out. Here was my process for figuring it out:
The pascal triangle is based on the formula n(n-1)/2 which gives the following pyramid:
1
23
456
etc...
So, this means we need to print the name 'anony' as:
a
no
ny-
I am using a '-' to fill in empty spaces for the last line as not every name is equivalent to a triangular number. Then I reasoned through printing out each line, and what the start and end point of each line should be. This led me to look at the DIFFERENCE between the starting points on each line, as well as the ending points on each line. This gave me the following breakdown:
Start points: 1 2 4 7 11
Start Dif: 1 2 3 4
End points: 1 3 6 10 15
End Dif: 2 3 4 5
So my start point increases by a linear incremental amount each iteration. My end point also increases by a linear incremental amount on each iteration, but by an amount that is 1 larger than the start difference.
For simplification, we handle the single case as a stand alone so we don't have to fight with arrays being 0 indexed and how that can create confusion with our algorithm. That being said, here is my algorithm for this process:
1. Declare a counter, start, and end and set all to 0
2. Print the first character and move to the next line
3. While end is less than the length of the name, do steps 4-8
4. Increment count by 1
5. Increment start by count
6. Increment end by count + 1
7. Loop through and print all chars from start to end (inclusive), if you go past the end of the name array, then print a '-' to finish the triangle (you could also return early and leave a pyramid with missing blocks in its foundation)
8. Move to next line and return to step 3
Below is my sample code along with a sample output:
String name = "WhatALongName";
int start = 0, count = 0, end = 0;
System.out.println(name.charAt(0));
while(end < name.length()){
count++;
start += count;
end += (count + 1);
for(int i = start; i <= end; i++){
if(i < name.length()){
System.out.print(name.charAt(i));
}
else{
System.out.print("-");
}
}
System.out.println();
}
Output:
W
ha
tAL
ongN
ame--
Here it s a complete code ..just copy this and paste in browxy
Many thanx to - masterjaso for ths good code,,,,
import java.io.*;
public class Stringex{
public static void main(String args[]){
String name = "SachinTendulkar";
int start = 0, count = 0, end = 0;
System.out.println(name.charAt(0));
while(end < name.length()){
count++;
start += count;
end += (count + 1);
for(int i = start; i <= end; i++){
if(i < name.length()){
System.out.print(name.charAt(i));
}
else{
System.out.print("-");
}
}
System.out.println();
}}}
output:
S
ac
hin
Tend
ulkar
------
OK, I think this is a pretty simple problem, I've run it on Visual Studio 2010
My code is shown below:
void printPascalTrangle(string str)
{
int length = str.size();
int lineCount = 1;
int i = 0;
/* output the string at first */
for (string::iterator it = str.begin(); it != str.end(); it ++)
cout << *it;
cout << endl;
/* print pascal trangle */
while (length > 0)
{
for (int j = 0; j < lineCount; j ++)
{
cout << str.at(i);
if (++ i == str.size() - 1)
break;
}
cout << endl;
lineCount ++;
length -= lineCount;
}
}
while(str[l]!='\0')
- mahima September 13, 2013{
for(int i=0;i<n;i++)
{
for(int j=0;j<=n-i-1;j++)
cout<<" ";
for(j=0;j<=i;j++)
{
cout<<str[l++]<<" ";
}
cout<<"\n";
}}}
this will print your name in pascal triangle!