Google Interview Question for Software Engineer / Developers






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

Basically, keep track of the number of columns for each row in some list structure, then sort the list and grab the highest value.

For each row (<TR>) count the header and detail elements (<TH> and <TR>); for each one, increment the corresponding entry in your list structure. The trick is to look for ROWSPAN attributes within the TH and TD elements; if you find such a rowspan attribute, increment the appropriate number of subsequent entries in your list structure, as well.

Semi-Pseudo code:

ArrayList<int> columnCounter = new ArrayList<int>();
int rowIndex = 0;
find the start of the table;
For each "<TR " or "<TR>" {
    For each "<TH ", "<TH>", "<TD " or "<TD>" after the closing ">" of the TR {
        if (rowIndex + 1 > columnCounter.size())
            columnCounter.add(0);
        columnCounter[rowIndex]++;
        if ROWSPAN before the closing ">" of the TH or TD and if ROWSPAN > 1 {
            for (int i = 1; i < ROWSPAN Value; i++) {
                if (rowIndex + i + 1 > columnCounter.size())
                    columnCounter.add(0);
                columnCounter[rowIndex + i]++;
            }
        }
    }
    rowIndex++;
}

columnCounter.sort();
if !columnCounter.isEmpty()
  return columnCounter[columnCounter.size() - 1];
return 0;

When finding the TR, TH, TD and ROWSPAN strings, be sure to ignore commented or quoted instances

- Anonymous August 22, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Using Jquery:
alert(numCol("table") + " is the max number of cols");
function numCol(table) {
var maxColNum = 0;
var i=0;
var trs = $(table).find("tr");

// var thead = $(table).find("tr").find("th");
for ( i=0; i<trs.length; i++ ) {
var span = $(trs[i]).attr("colspan");
var thead= $(trs[i]).find("th");

if (thead)
{
if(span){

maxColNum += parseInt(span);
}
maxColNum++;
}
else {
maxColNum++;
}
}
return maxColNum;
}

- Sent October 26, 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