Hack4Hack

Daily Coding Quiz: Thursday, 19 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 traversal of a binary search tree visits the keys in sorted order?

    DSA · easy

    1. Preorder
    2. Postorder
    3. Inorder
    4. Level order

    Answer: C. Inorder

    Explanation: Inorder visits left subtree, node, right subtree. In a BST every left key is smaller and every right key larger, so the output is sorted.

  3. 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.

  4. A Union-Find (disjoint set) structure is the standard helper in which algorithm?

    DSA · medium

    1. Dijkstra’s shortest path
    2. Kruskal’s minimum spanning tree
    3. Binary search
    4. KMP string matching

    Answer: B. Kruskal’s minimum spanning tree

    Explanation: Kruskal’s adds edges in order of weight and must check quickly whether an edge joins two different components. Union-Find answers that in near O(1).

  5. Which array is a valid min-heap (array form, children of i at 2i+1 and 2i+2)?

    DSA · medium

    1. [1, 3, 2, 7, 4, 5]
    2. [1, 5, 2, 3, 6, 4]
    3. [2, 1, 3, 4, 5, 6]
    4. [1, 3, 2, 7, 4, 1]

    Answer: A. [1, 3, 2, 7, 4, 5]

    Explanation: In [1,3,2,7,4,5] every parent is ≤ its children (1≤3,2; 3≤7,4; 2≤5). In the second, 5 > 3; in the third, 2 > 1; in the fourth, 2 > 1 at index 5.

  6. 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.

  7. What is the amortised time per append to a dynamic array that doubles its capacity when full?

    DSA · hard

    1. O(1)
    2. O(log n)
    3. O(n)
    4. O(√n)

    Answer: A. O(1)

    Explanation: Copying happens at sizes 1, 2, 4, …, n, totalling under 2n copies for n appends, so each append costs O(1) amortised, though a single append can be O(n).

  8. A segment tree over n elements supports range-sum queries and point updates in:

    DSA · hard

    1. O(1) and O(n)
    2. O(log n) each
    3. O(√n) each
    4. O(n) and O(1)

    Answer: B. O(log n) each

    Explanation: The tree has height O(log n). A query combines O(log n) nodes, and an update changes one leaf and its O(log n) ancestors.

  9. What is printed?

    DSA · hard

    def lis(a):
        import bisect
        tails = []
        for x in a:
            i = bisect.bisect_left(tails, x)
            if i == len(tails):
                tails.append(x)
            else:
                tails[i] = x
        return len(tails)
    
    print(lis([10, 9, 2, 5, 3, 7, 101, 18]))
    1. 3
    2. 4
    3. 5
    4. 8

    Answer: B. 4

    Explanation: This is the O(n log n) Longest Increasing Subsequence. One LIS is 2, 3, 7, 18 (or 2, 5, 7, 101), length 4.

  10. Which algorithm can find shortest paths when some edge weights are negative (but there is no negative cycle), and also detect a negative cycle?

    DSA · hard

    1. Dijkstra’s
    2. Bellman–Ford
    3. Prim’s
    4. BFS

    Answer: B. Bellman–Ford

    Explanation: Bellman–Ford relaxes every edge V−1 times. If a V-th pass still improves a distance, there is a negative cycle. Dijkstra’s can give wrong answers with negative edges.

Play the next Daily Quiz at 4 PM IST