System Design and Architecture 1 — Questions and Answers
Question 1: You are given an array [2, 7, 11, 15] and a target 9. Which pair of indices will sum up to the target?
- [0, 2]
- [0, 1] (Correct answer)
- [1, 2]
- [2, 3]
Correct answer: [0, 1]
To find the pair of indices that sum to the target, we examine the elements of the array [2, 7, 11, 15]. The element at index 0 is 2, and the element at index 1 is 7. Adding these two values (2 + 7) equals 9, which matches the target. Therefore, the indices [0, 1] correspond to the elements that sum to the target.
Question 2: Which of the following sorting algorithms has the best average-case time complexity?
- Bubble Sort
- Quick Sort
- Merge Sort (Correct answer)
- Selection Sort
Correct answer: Merge Sort
While Quick Sort often performs very well in practice with an average-case time complexity of O(n log n), its worst-case complexity is O(n^2). Merge Sort, on the other hand, consistently maintains an O(n log n) time complexity in its best, average, and worst-case scenarios. This makes Merge Sort a more reliably efficient choice for its average-case performance compared to the other options listed.
Question 3: Which type of SQL join returns all records from the left table and matching records from the right table?
- INNER JOIN
- LEFT JOIN (Correct answer)
- RIGHT JOIN
- FULL OUTER JOIN
Correct answer: LEFT JOIN
A `LEFT JOIN` (also known as `LEFT OUTER JOIN`) returns all rows from the "left" table (the first table mentioned in the `FROM` clause) and the matching rows from the "right" table. If there is no match in the right table, `NULL` values are returned for the columns from the right table. This ensures that no data from the left table is lost.
Question 4: What is the time complexity of traversing a singly linked list with n elements?
- O(1)
- O(log n)
- O(n) (Correct answer)
- O(n²)
Correct answer: O(n)
To traverse a singly linked list, you must visit each node sequentially from the head to the tail. In a list with 'n' elements, this requires 'n' steps, as you can only move forward one node at a time. Therefore, the time complexity for traversing the entire list is directly proportional to the number of elements, which is O(n).
Question 5: Consider the following Python function: def add_numbers(a, b): return a + b print(add_numbers(2))
- TypeError (Correct answer)
- ValueError
- SyntaxError
- IndexError
Correct answer: TypeError
The `add_numbers` function is defined to accept two arguments, `a` and `b`. However, when `print(add_numbers(2))` is called, only one argument is provided. Python will raise a `TypeError` because the function call is missing the required second positional argument `b`.
You are given an array [2, 7, 11, 15] and a target 9.
Which pair of indices will sum up to the target?