Interview Question


Country: United States




Comment hidden because of low score. Click to expand.
0
of 0 vote

the idea for this one is binary search to find low bound (row) and upper bound (col) for the
2-d array

private int getlength(String target){
		int count = 0 ;
		for (char c : target.toCharArray()) {
			if (c != ' ') {
				count++;
			}
		}
		return count ;
	}
	
	public void createSquare (String target){
		int len = getlength(target) ;		
		if (len == 0) {
			return ;
		}
		long low = 1 , high = len ;
		while (low + 1 < high) {
		  long mid = low + (high - low) / 2 ;
		  if (mid * mid < len ) {
			  low = mid ;
		  } else{
			  high = mid ;
		  }
		}
		char [][] ch = null ;
		int row = 0 , col = 0 ;
		if (high * high == len) {
			row = (int) high ;
			col = (int) high ;
			ch = new char[row][col];
		} else if (low * low == len) {
			row = (int) low ;
			col = (int) low ;
			ch = new char[(int)row][(int)col];
		} else{
			row = (int) low ;
			col = (int) high ;
			ch = new char[(int)row][(int)col];
		}
		createMatrix (ch, target);
		display (ch);
	}
	
	private void createMatrix (char [][] matrix, String word){
		int row = matrix.length , col = matrix[0].length;
		int i = 0 , j = 0 , p = 0;
		while (i < row) {
			j = 0 ;
		  	while (p < word.length() && j < col) {
		  		if (word.charAt(p) != ' ') {
		  		   matrix[i][j++] = word.charAt(p) ;	
		  		}
		  		p++;
		  	}
		  	i++;
		}
	}
	
	private void display (char [][] matrix){
		int row = matrix.length , col = matrix[0].length;
		for (int i = 0 ; i < row ; ++i)  {
		 for (int j = 0 ; j < col ; ++j)  {
			 if (matrix[i][j] != '\u0000') {
				 System.out.print(matrix[i][j]);	 
			 }
			 
		 }
		 System.out.println();
		}
	}

- Scott March 04, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void main()
{
   char* input = "if man was meant to stay on the ground god would have given us roots";
   char* spaceless = new char[strlen(input)];
   for (int i = 0, j = 0; !i || input[i-1]; i++)
      if (input[i] != ' ')
         spaceless[j++] = input[i];
   int numRow = (int)sqrt((float)strlen(spaceless));
   int lastRow = strlen(spaceless) - numRow*numRow + 1;
   int numCol = numRow + (lastRow != 0);
   for (int i = 0; i < numCol; i++, printf(" "))
      for (int j = 0; j < numRow; j++)
         if ((j < numRow - 1) || (i < lastRow))
            printf("%c", spaceless[j*numCol + i]);    
   getch();
};

output:

imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau

}

- tjcbs2 March 05, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

StringBuilder resultString = new StringBuilder();
		String[] words = sentence.split("\\s+");
		StringBuilder allWords = new StringBuilder(sentence.length());
		for (String word : words){
			allWords.append(word.toLowerCase());
		}
		int counter = 0 , allWordsLength = allWords.length();

		while (counter < allWordsLength){
			int upper = counter + 8;
			if (upper < allWordsLength){
				resultString.append(allWords.substring(counter, upper)+" ");
			}else{
				resultString.append(allWords.substring(counter));
			}
			counter += 8;
		}
		return resultString.toString();

- Aneesh Kulkarni March 08, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Is there no way to edit posts?

** surround the above code between the next line

public static String getSquareCode(String sentence) {

and

}

- Anonymous March 08, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One classic method for composing secret messages is called a square code. The spaces are removed from the English text and the characters are written into a square (or rectangle). For example, the sentence "If man was meant to stay on the ground god would have given us roots" is 54 characters long, so it is written into a rectangle with 7 rows and 8 columns.
ifmanwas
meanttos
tayonthe
groundgo
dwouldha
vegivenu
sroots
The coded message is obtained by reading down the columns going left to right. For example, the message above is coded as:

- leelasagar October 20, 2016 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include <stdio.h>
#include "square.h"

int main () {
char *msg;
int length;

for (int i = 0 ; i < get_number_of_messages(); i++) {
msg = read_message(i);
length = get_message_length(i);

for (int i = 0; i < length; i++) {
printf("%c", msg[i]);
}

printf("\n");
}

return 0;
}

- abhiram October 21, 2022 | 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