Hack4Hack

Daily Coding Quiz: Tuesday, 3 November 2026

All 10 coding questions with answers and explanations. 0 players took part.

  1. How many edges does a tree with n nodes have?

    DSA · easy

    1. n
    2. n − 1
    3. n + 1
    4. 2n

    Answer: B. n − 1

    Explanation: A tree is connected and acyclic: every node except the root has exactly one edge to its parent, giving n − 1 edges.

  2. Which statement removes all rows from a table but keeps the table itself?

    SQL · easy

    1. DROP TABLE
    2. DELETE TABLE
    3. TRUNCATE TABLE
    4. REMOVE TABLE

    Answer: C. TRUNCATE TABLE

    Explanation: TRUNCATE empties the table quickly (and in MySQL resets AUTO_INCREMENT). DROP deletes the table definition too. DELETE without WHERE also removes all rows, row by row.

  3. What does this return?

    SQL · easy

    -- Table employees(id, name, dept, salary):
    -- (1, Asha,  Eng,   90)
    -- (2, Ravi,  Eng,   70)
    -- (3, Meera, Sales, 60)
    -- (4, Kabir, Sales, 60)
    -- (5, Neha,  HR,    NULL)
    SELECT COUNT(*), COUNT(salary) FROM employees;
    1. 5, 5
    2. 5, 4
    3. 4, 4
    4. 4, 5

    Answer: B. 5, 4

    Explanation: COUNT(*) counts rows (5). COUNT(column) skips NULLs, and Neha’s salary is NULL, so 4.

  4. What does this return for nums = [2, 7, 11, 15], target = 9?

    DSA · medium

    def solve(nums, target):
        seen = {}
        for i, x in enumerate(nums):
            if target - x in seen:
                return [seen[target - x], i]
            seen[x] = i
        return []
    1. [0, 1]
    2. [1, 0]
    3. [2, 7]
    4. []

    Answer: A. [0, 1]

    Explanation: At i=1 (x=7), 9−7=2 is already in seen at index 0, so it returns [0, 1]. This is the O(n) hash-map solution to Two Sum.

  5. Which query finds the second-highest distinct salary?

    SQL · medium

    1. SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)
    2. SELECT salary FROM employees ORDER BY salary LIMIT 2
    3. SELECT MAX(salary) - 1 FROM employees
    4. SELECT salary FROM employees WHERE salary = 2

    Answer: A. SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees)

    Explanation: Take the maximum among salaries below the overall maximum. ORDER BY salary DESC LIMIT 1 OFFSET 1 also works only if there are no duplicates at the top; DISTINCT fixes that.

  6. What does this return?

    SQL · medium

    SELECT COALESCE(NULL, NULL, 'x', 'y'), NULLIF(5, 5), IFNULL(NULL, 0);  -- MySQL
    1. x, NULL, 0
    2. NULL, 5, 0
    3. y, 0, NULL
    4. x, 5, 0

    Answer: A. x, NULL, 0

    Explanation: COALESCE returns the first non-NULL argument. NULLIF(a, b) is NULL when a = b. IFNULL(a, b) is MySQL’s two-argument COALESCE.

  7. A composite index exists on (last_name, first_name). Which WHERE clause can use it best?

    SQL · medium

    1. WHERE first_name = 'Ravi'
    2. WHERE last_name = 'Shah'
    3. WHERE UPPER(last_name) = 'SHAH'
    4. WHERE first_name LIKE '%vi'

    Answer: B. WHERE last_name = 'Shah'

    Explanation: A B-tree index on (a, b) is sorted by a first, so it helps filters on its leftmost column. Wrapping the column in a function, or filtering only on b, generally prevents its use.

  8. You need the k largest of n numbers arriving as a stream, using little memory. The best approach:

    DSA · hard

    1. Sort everything at the end: O(n log n) time, O(n) memory
    2. A min-heap of size k: O(n log k) time, O(k) memory
    3. A max-heap of all n elements
    4. A hash set of size k

    Answer: B. A min-heap of size k: O(n log k) time, O(k) memory

    Explanation: Keep the k largest so far in a min-heap: if a new number beats the heap’s minimum, replace it. Only k items are ever stored.

  9. Which query returns the highest-paid employee in every department (ties included)?

    SQL · hard

    1. SELECT dept, name, MAX(salary) FROM employees GROUP BY dept
    2. SELECT dept, name, salary FROM (SELECT *, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) r FROM employees) t WHERE r = 1
    3. SELECT dept, name, salary FROM employees ORDER BY salary DESC LIMIT 1
    4. SELECT DISTINCT dept, MAX(name) FROM employees

    Answer: B. SELECT dept, name, salary FROM (SELECT *, RANK() OVER (PARTITION BY dept ORDER BY salary DESC) r FROM employees) t WHERE r = 1

    Explanation: Ranking within each department and keeping rank 1 returns the right name with ties. The GROUP BY version selects a name not tied to the MAX (an error under ONLY_FULL_GROUP_BY, an arbitrary name otherwise).

  10. Which isolation level prevents dirty reads, non-repeatable reads and phantom reads (per the SQL standard)?

    SQL · hard

    1. READ UNCOMMITTED
    2. READ COMMITTED
    3. REPEATABLE READ
    4. SERIALIZABLE

    Answer: D. SERIALIZABLE

    Explanation: Only SERIALIZABLE forbids all three in the standard. (MySQL InnoDB’s default REPEATABLE READ also blocks most phantoms using next-key locks.)

Play the next Daily Quiz at 4 PM IST