Apple Interview Question for Staff Engineers


Country: United States




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

Since we know the maximum result can be three digits i.e 12 * 12 = 144, we can use the string formatting function when printing our output to correctly align the output. In this case, we have 3 digits as our max length, so we can format it in that particular way.

Solution:

def printMultiplicationTableFor12():
  for row in range(1, 13):
    for col in range(1, 13):
      print('{:3} '.format(row * col),end='')
    print()

Test code:

printMultiplicationTableFor12()

'''
Output:
  1   2   3   4   5   6   7   8   9  10  11  12 
  2   4   6   8  10  12  14  16  18  20  22  24 
  3   6   9  12  15  18  21  24  27  30  33  36 
  4   8  12  16  20  24  28  32  36  40  44  48 
  5  10  15  20  25  30  35  40  45  50  55  60 
  6  12  18  24  30  36  42  48  54  60  66  72 
  7  14  21  28  35  42  49  56  63  70  77  84 
  8  16  24  32  40  48  56  64  72  80  88  96 
  9  18  27  36  45  54  63  72  81  90  99 108 
 10  20  30  40  50  60  70  80  90 100 110 120 
 11  22  33  44  55  66  77  88  99 110 121 132 
 12  24  36  48  60  72  84  96 108 120 132 144 
'''

- prudent_programmer March 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The important part was formatting of the result

- kay March 16, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

IntStream.range(1, 13).forEach(i->{IntStream.range(1,13).forEach(j->{System.out.print(i*j+" ");});System.out.println("");});

- Vinayaka Sridhara March 17, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

/* print n*n multiplication table, and format output based on input */
    public static void printTable(int n){
        int highest = n*n;
        int tens = 1;

        highest /= 10;
        while (highest != 0){
            tens++;
            highest /= 10;
        }

        String format = "%" + tens + "s ";

        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                System.out.printf(format, i*j);
            }
            System.out.println();
        }
    }

- dev.yeison March 18, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

no need for a loop to figure out how many digits you need, just use log10 + 1

- th1rtyf0ur January 29, 2019 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

def print_m_table(n):
char_len = len(str(n*n))+1
for row in range(1, n+1):
for col in range(1, n+1):
print(str(row*col).rjust(char_len), end="")
print()

print_m_table(20)

- bob March 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

static void print(int n){
for(int j = 1; j<=n; j++){
for(int i = 1; i<=n; i++){
System.out.print (i * j );
System.out.print("\t");
}
System.out.println();
}
}

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

python code

def print_table():
    for i in range(1,13):
        for j in range(1,13):
            print(j*i,"",end="")
        print("")

- ganjc March 22, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

function printMultiplicationTable(): void {
    for (let x=1; x<13; x++) {
        const arr = [];
        for (let y=1; y<13; y++) {
            arr.push(x*y);
        }
        console.log(arr.toString());
    }
}

- andrei.hetman July 19, 2018 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

import Foundation

for y in 1 ... 12 {
for x in 1 ... 12 {
print(String(format:"%3d", x * y) + " ", terminator:"")
}
print()
}

- Anonymous January 28, 2019 | 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