📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!


    Question

    In Python, which of the following built-in data

    structures can be most efficiently used to implement a stack, and why?
    A list using append() and pop() from the end, due to O(1) average time complexity. Correct Answer Incorrect Answer
    B collections.deque using append() and popleft(), due to O(1) average time complexity. Correct Answer Incorrect Answer
    C list using insert(0, item) and pop(0), due to O(1) average time complexity. Correct Answer Incorrect Answer
    D tuple due to its immutability and fast access. Correct Answer Incorrect Answer
    E set due to its unique element property. Correct Answer Incorrect Answer

    Solution

    In Python, list.append() and list.pop() (without an index) operate on the end of the list, which is an O(1) average time complexity operation, making list an efficient choice for stack implementation. Using insert(0, item) or pop(0) would involve shifting all other elements, resulting in O(N) complexity.

    Practice Next
    ask-question