Google Interview Question for Java Developers


Country: United States




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

using System;
using System.Globalization;
using System.Collections.Generic;
using System.Text;
using System.Linq;
					
public class Program
{
	public static void Main()
	{
		var text = "Thank you for shopping at the XYZ Store.\nYour order has been processed successfully.\n";
		Console.WriteLine(StringWrap(text,20));
	}
	
	public static string StringWrap(string text, int characters) {		
		StringBuilder sb = new StringBuilder();
		
		string[] lines =text.Split('\n');
				
		foreach(var line in lines) {
			sb.Append(StringWrapLine(line, characters));
			sb.Append("\n");			
		}
		
		return sb.ToString();	
	}
	
	public static string StringWrapLine(string text, int characters) {		
		string[] words =text.Split(' ');
				
		StringBuilder sb = new StringBuilder();
		int sum = 0;
		
		foreach(var word in words) {
			if(sum + word.Length <= characters) {
				sb.Append(word + " ");
				sum += word.Length + 1;										
			}
			else {
				sb.Length--;
				sb.Append("\n");	
				sum = 0;
				sb.Append(word + " ");
				sum += word.Length + 1;										
			}			
		}
		
		return sb.ToString();	
	}
}

- C# solution January 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class Program
{
	public static void Main()
	{
		var text = "Thank you for shopping at the XYZ Store.\nYour order has been processed successfully.\n";
		Console.WriteLine(StringWrap(text,20));
	}
	
	public static string StringWrap(string text, int characters) {		
		StringBuilder sb = new StringBuilder();
		
		string[] lines =text.Split('\n');
				
		foreach(var line in lines) {
			sb.Append(StringWrapLine(line, characters));
			sb.Append("\n");			
		}
		
		return sb.ToString();	
	}
	
	public static string StringWrapLine(string text, int characters) {		
		string[] words =text.Split(' ');
				
		StringBuilder sb = new StringBuilder();
		int sum = 0;
		
		foreach(var word in words) {
			if(sum + word.Length <= characters) {
				sb.Append(word + " ");
				sum += word.Length + 1;										
			}
			else {
				sb.Length--;
				sb.Append("\n");	
				sum = 0;
				sb.Append(word + " ");
				sum += word.Length + 1;										
			}			
		}
		
		return sb.ToString();	
	}
}

- Anonymous January 30, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public static void wrapText(String text, int characters){
		int start = 0;
		int end = 0;
		int counter = 0;
		for (int i = 0; i < text.length(); i++) {
			if(i == text.length()-1){
				System.out.println(text.substring(start, text.length()));
				break;
			}
			if(counter< characters){
				if(text.charAt(i)==' ')
					end = i;
				counter++;
			}
			else{
				if(text.charAt(i)==' '){
					System.out.println(text.substring(start, i));
					start = i+1;
					end =i+1;
				}else{
					System.out.println(text.substring(start, end));
					start = end+1;
					i = end;
					end = start;
				}
				counter = 0;

			}	
		}

- Tarun February 02, 2018 | 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