Interview Question for Java Developers


Country: India
Interview Type: Written Test




Comment hidden because of low score. Click to expand.
1
of 3 vote

A naive solution is to:

1. Read the file line by line.
2. Print the characters of the ith line as ith column in the output file.
Printing in ith column entails
a. Reading the output file line by line
b. If there is no corresponding line in the output file for the kth character ith line of input file,
create a new line, fill with (k-1) spaces and add the kth character
c. if there is a corresponding(kth) line in the output file and if the length is < k-1, fill with spaces upto (k-1)st character, then append the kth character of the ith line from the input
d else append the kth character

solution to this problem can actually depend on questions like - does the whole file fit into main memory? What is the required time complexity?? etc

- Murali Mohan June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

1. Create a matrix from the characters from the input file. For example your example will generate this 3x3 matrix below. Each row is a line and each character will be in different columns including the spaces, so the number of columns will be the maximum line length.

| a b c |
| d e f | = matrix[3][3]
| g h |

2. Transpose the matrix:

public static char[][] transpose(char[][] matrix) {
	for (int i = 0; i < matrix.length; i++) {
		for(int j =0; j < matrix[0].length; j++) {
			if (i > j) {
				// swap the characters in diagonal
				char temp = matrix[i][j];
				matrix[i][j] = matrix[j][i];
				matrix[j][i] = temp;
			}
		}
	}
		
	return matrix;
}

3. Then print the matrix.

- oOZz June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int main()
{
vector<string> vec;
while(EOF)
{
int row =0;
row++;
string str = readLine(file);
for(it = str.begin();it = str.end();it++)
{
if(vec.size()<i)
{
string temp ;
for(row times)
{
temp +=' ';// add spaces for places when cells of this column is empty
}
temp += str.at(i);
vec.push_back(temp);
}
else
{
if(vec[i].length()<row-1)
{
for(vec[i].length()-row+1 times)
vec[i] += ' '; // add spaces for cells of this coloumn where no char was there
}
vec[i] +=str.at(it);
}
}
}

}

- Aditya Vemuri June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
main(){
FILE *fp;
int i=0,j=0,items,max=0;
char ch;
char arr[1000][1000];
fp=fopen("filename.txt","r");
while((ch=fgetc(fp))!=EOF){
if(ch!='\n'){
arr[i][j++]=ch;
max=(max<j)?j:max;
}
else{
arr[i][j]='\0';
i++;
j=0;
}
items=i;
}
while(j!=max){
for(i=0;i<items;i++){
printf("%c",arr[i][j]);
}
printf("\n");
j++;
}
}

- Anonymous June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

y don't we create the array dynamically............if we create the array dynamically we can solve the problem with less space complexity......

- Anonymous June 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

i guess we can do that
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
{ }char **a,str[100];
{ }a=(char**)malloc(sizeof(char*)*10);
{ }printf("%d\n",sizeof(char*));
{ }int row=0,col,max=0,i,j;
{ }int c;
{ } FILE *f=fopen("lines.txt","r");
{ } fscanf(f,"%[^\n]s",str);
{ } c=fgetc(f);
{ } int cnt=0;
{ } while(c!=EOF)
{ } {
{ } col=strlen(str);
{ }a[row]=(char*)malloc(sizeof(char)*col);
{ } a[row][0]=(char)col;
{ } for(j=1;j<=col;j++)
{ }{
{ }a[row][j]=str[j-1];
}
{ } if(col>max)
{ } max=col;
{ }row++;
{ } fscanf(f,"%[^\n]s",str);
{ } c=fgetc(f);
{ }}
{ }int k;
{ }char b[max][row];
{ }for(i=0;i<row;i++)
{ }{
{ }col=(int)a[i][0];
{ }for(j=1;j<=col;j++)
{ }{
{ }b[j-1][i]=a[i][j];
{ }}
{ }j--;
{ } while(j<max)
{ }{
{ }b[j++][i]='\0';
{ }}
{ }}
{ }for(i=0;i<max;i++)
{ }{
{ }for(j=0;j<row;j++)
{ }printf("%c",b[i][j]);
{ }printf("\n");
{ }}

{ }}

- Anonymous June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

{
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main()
{
char **a,str[100];
a=(char**)malloc(sizeof(char*)*10);
printf("%d\n",sizeof(char*));
int row=0,col,max=0,i,j;
int c;
FILE *f=fopen("lines.txt","r");
fscanf(f,"%[^\n]s",str);
c=fgetc(f);
int cnt=0;
while(c!=EOF)
{
//printf("%s \n",str);
col=strlen(str);
a[row]=(char*)malloc(sizeof(char)*col);
a[row][0]=(char)col;
for(j=1;j<=col;j++)
{
a[row][j]=str[j-1];
//printf("%c",a[row][j]);
}
printf("\n");
if(col>max)
max=col;
row++;
fscanf(f,"%[^\n]s",str);
c=fgetc(f);
}
int k;
char b[max][row];
for(i=0;i<row;i++)
{
col=(int)a[i][0];
for(j=1;j<=col;j++)
{
//printf("%c",a[i][j]);
b[j-1][i]=a[i][j];
}
j--;
while(j<max)
{
b[j++][i]='\0';
}
}
for(i=0;i<max;i++)
{
for(j=0;j<row;j++)
printf("%c",b[i][j]);
printf("\n");
}

}
}
This will work, sorry for the previous code

- Anonymous June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

We can create an array the size of string in first line. Each element of this array is a StringBuffer object. Now, we read the file starting from the first line...... append the i-th char in the string to the i-th entry of array. Go on this way for all the lines.... then you can output the entries of array in different lines. All done.

- Fr0d0 June 11, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

For calculating transpose we need to use extra space, right?

- Anonymous June 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Its an example of radix sort. By radix sort principle we can do it very easily.

- Anonymous June 12, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

File f = new File("Data.txt");

BufferedReader br = new BufferedReader(new FileReader(f));

List<Integer> sizeList = new LinkedList<Integer>();

List<String> lines = new LinkedList<String>();

String line = null;

//Read from the file and add line to the List.
while ((line = br.readLine()) != null) {
lines.add(line);
sizeList.add(line.length());
}

//Sort the sizeList to get the max length of a line at the last index.
Collections.sort(sizeList);

//Get the value at the last index of the Size List
int largestCharPosition = sizeList.get(sizeList.size() - 1);

int j = 0;
while(j < largestCharPosition) {
for(int i = 0; i < lines.size(); i++) {
int length = lines.get(i).length();
if(length > j) {
System.out.print(lines.get(i).charAt(j));
}
}
System.out.println();
j++;
}

- 2010prashant September 12, 2013 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More