Sollicitatievraag bij Fidelity Investments

Code a multiplication table.

Antwoorden op sollicitatievragen

Anoniem

16 nov 2018

public static void main(String []args){ for(int i = 1; i<=9; i++){ for(int j=1; j<=9; j++ ){ System.out.printf("%4d", i*j); } System.out.println(); } }

Anoniem

11 dec 2020

// An alternate O(n^2) solution. I feel like you could make this O(n log n) somehow if you stored previous multiplication pairs in some data structure. int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; int colCount = 1; int rowCount = 1; int innerCount = 0; while (rowCount != numbers.length + 1) { System.out.printf("%4d", numbers[innerCount++] * colCount); if (innerCount == numbers.length) { innerCount = 0; colCount++; rowCount++; System.out.println(""); } }

Anoniem

11 dec 2020

Adding to above answer. You can think of it like storing results of multiplying: 1 -> 9 2 -> 9 3 -> 9 ... 8-> 9 9 -> 9 So that you're only computing the values once. So if you needed 9 * 2 (larger times smaller), you would look up the where computations of 2 (the smaller number) are stored and grab the value of 9. A 2D jagged array might could work for this.