Google Interview Question


Country: United States




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

function intoTable(list, width) {

    let lines = [];

    let lineCounter = 0;
    let currLine = "";
    
    for (let i = 0; i < list.length; i++) {
        let word = list[i];
        
        if(lineCounter + word.length + 1 < width) {
            currLine += lineCounter ==  0? word: " " + word;
            lineCounter += word.length;
        } else {
            currLine += " |";
            lines.push(currLine);
            
            currLine = "";
            lineCounter = 0;
        }
    }

    currLine += " |";
    lines.push(currLine);

    return lines.join("\n");
}

- Anonymous November 19, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function intoTable(list, width) {

    let lines = [];

    let lineCounter = 0;
    let currLine = "";
    
    for (let i = 0; i < list.length; i++) {
        let word = list[i];
        
        if(lineCounter + word.length + 1 < width) {
            currLine += lineCounter ==  0? word: " " + word;
            lineCounter += word.length;
        } else {
            currLine += " |";
            lines.push(currLine);
            
            currLine = "";
            lineCounter = 0;
        }
    }

    currLine += " |";
    lines.push(currLine);

    return lines.join("\n");
}

- amr November 19, 2022 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

var Intotable(list, width){
let linecount =0
let s= "
let rest = []
for(let i = 0;i<list.length; i++){
let word = list[i]
if(linecount+word.length+1<width){
s=s+linecount=0?word: ' '+word
linecount+=word.length
}else{ s+="|"
linecount=0
rst.push(s)
s=""
}
}
return rst.join("\n")
}

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

StringBuilder output = new StringBuilder();
        int appendedSoFar = 0;
        for (String str : strings) {
            int remaining = charPerLine - appendedSoFar;
            if (remaining >= str.length()) {
                if(remaining != charPerLine) {
                    output.append(" ");
                }
                output.append(str);
                appendedSoFar += str.length() + 1;
            } else {
                output.append("\n");
                output.append(str);
                appendedSoFar = str.length() + 1;
            }
        }
        return output.toString();

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

public String populateTable(String[] strings, int charPerLine) {
        StringBuilder output = new StringBuilder();
        int appendedSoFar = 0;
        for (String str : strings) {
            int remaining = charPerLine - appendedSoFar;
            if ((remaining - 2) >= str.length()) {
                // Beginning of the line.
                if(remaining != charPerLine) {
                    output.append(" ");
                }
                output.append(str);
                appendedSoFar += str.length() + 1;
            } else {
                output.append("|");
                output.append("\n");
                output.append(str);
                appendedSoFar = str.length() + 1;
            }
        }
        return output.toString();

}

- pr July 12, 2023 | 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