Interview Question
Java DevelopersCountry: India
Interview Type: Written Test
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.
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);
}
}
}
}
#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++;
}
}
y don't we create the array dynamically............if we create the array dynamically we can solve the problem with less space complexity......
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");
{ }}
{ }}
{
#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
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.
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++;
}
A naive solution is to:
- Murali Mohan June 10, 20131. 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