Google Interview Question: Write a function that takes t... | Glassdoor.co.uk

Interview Question

Software Engineer Interview London, England

Write a function that takes the ordinal number of a column

  in a spreadsheet and returns the label of that column: i.e. 1 -> A 2 -> B, 26 -> Z, 27->AA
Answer

Interview Answer

7 Answers

0

!!!! There can be some bugs not fully tested use at your own risk =)...

import java.util.ArrayList;

public class OrdinalToColumn {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int valueToConvert = 64;
        System.out.println(ordinalToColumnConversion(valueToConvert));
    }

    private static char[] ordinalToColumnConversion(int valueToConvert) {
        // TODO Auto-generated method stub
        ArrayList numbersReverse = new ArrayList();
        int sizeOfArr;
        int curIndex, rollBackIndex;
        while(valueToConvert>0){ // base conversion
            numbersReverse.add(valueToConvert % 26);
            valueToConvert /= 26;
        }
        sizeOfArr = numbersReverse.size();
        curIndex = rollBackIndex = 0;
        while(curIndex rollBackIndex){ // borrowing can lead some cascading operation we have to roll back until all digits are more than 0
                    int barrowNumber = numbersReverse.get(tempIndx); // barrow from previous
                    barrowNumber--;
                    numbersReverse.set(tempIndx, barrowNumber);
                    tempIndx--;
                    int nextNumber = numbersReverse.get(tempIndx); // add to the current one
                    nextNumber += 26;
                    numbersReverse.set(tempIndx, nextNumber);
                }
            }
            else {
                curIndex++;
            }

        }
        if(sizeOfArr > 0 && numbersReverse.get(sizeOfArr-1) == 0){ // remove the highest significant bit if it is 0
            numbersReverse.remove(sizeOfArr-1);
            sizeOfArr--;
        }

        char [] returnNumbers = new char [sizeOfArr];
        for(int i = sizeOfArr-1, n=0; i>=0; --i, ++n){
            returnNumbers[n] = (char)(numbersReverse.get(i)+64); // converting to capital letter
        }
        return returnNumbers;
    }

}

interwee on 3 Jan 2012
0

Here my solution in Java:

public class SpreadSheetLabel {

    private static char LABEL[] = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};

    private static void printLabel(int n) {
        if (n = 26) {
            n /= 26;
            str.append(LABEL[(n-1) % LABEL.length]);
        }
        System.out.println(str.reverse());
    }

    public static void main(String[] args) {
        for(int i = 1; i <= 2600; i++) {
            printLabel(i);
        }
    }
}

Claudio on 3 Aug 2012
4

I haven't tried interwee's solution, but that code is so complex and long for such a simple problem I don't even wish to try and understand it :)

Anyways, here is my simple and elegant solution in Java:

    // Precondition: i >=1
    public static String excelNumbering(int i){
        if(i >=1 && i<= 26) return ""+(char)(i+64);
        else return excelNumbering((i-1)/26) + excelNumbering((i-1)%26+1);
    }

Rafal on 21 Nov 2012
0

In C++:

string excelNumber(int i) {
    string excelStr = "";
    int numAs = i / 26;
    int rest = i % 26;
    for (int j = 0; j < numAs; ++j) {
        excelStr += "A";
    }
    return excelStr + (char) (rest + 64);
}

Not sure what is meant to happen for error cases (e.g. 0).

Daniel on 30 Oct 2013
0

void calc_ord(int ord, char* out) {
  if (ord == 0) return;
  int tmp = ord;
  char *ptr = out;
  while (tmp <= 26) {
    *ptr++ = 'A';
    tmp -= 26;
  }
  *ptr++ = 'A' + tmp;
}

It's Simple on 26 Feb 2014
1

public String getName(int n)
{
 String ret="";
 while(n>0)
  {
    char c='A'+(n%26);
    ret=c+ret;
    n/=26;
  }
 return ret;
}

Lenny on 10 Sep 2014
0

here in OBJC. Is interesting that no one in the previous answers, is calculating the base system..everyone is hardcoding 26.

- (NSString *)lettersFromOrdinal:(NSUInteger)ordinal
{
    NSMutableString *letters = [NSMutableString new];

    NSUInteger base = 'Z'-'A'+1;
    NSUInteger labelLength = ceil(customLog(base, ordinal));

    for (NSInteger i = labelLength-1; i >= 0; i--) {
        NSUInteger pow_ = pow(base, i);
        NSUInteger res = ordinal/pow_;
        char letter = 'A' + res - 1;
        [letters appendFormat:@"%c", letter];
        ordinal -= res*pow_;
    }

    return [letters copy];
}

Matteo Gobbi on 8 Jul 2015

Add Answers or Comments

To comment on this, Sign In or Sign Up.