Bird
Raised Fist0
DSA Cprogramming~20 mins

Stack Implementation Using Array in DSA C - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Stack Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this stack push and pop sequence?
Consider a stack implemented using an array with a maximum size of 3. The following operations are performed in order:

push(10), push(20), pop(), push(30), push(40), pop(), pop()

What is the printed output of the popped elements?
DSA C
int stack[3];
int top = -1;

void push(int x) {
    if (top == 2) return; // stack full
    stack[++top] = x;
}

int pop() {
    if (top == -1) return -1; // stack empty
    return stack[top--];
}

int main() {
    push(10);
    push(20);
    printf("%d ", pop());
    push(30);
    push(40);
    printf("%d ", pop());
    printf("%d", pop());
    return 0;
}
A10 30 40
B10 40 30
C20 40 30
D20 30 40
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and returns it.
❓ Predict Output
intermediate
2:00remaining
What is the value of 'top' after these operations?
Given a stack implemented with an array of size 5, initially empty (top = -1). The operations are:

push(5), push(10), push(15), pop(), push(20), pop(), pop()

What is the value of 'top' after these operations?
DSA C
int stack[5];
int top = -1;

void push(int x) {
    if (top == 4) return;
    stack[++top] = x;
}

void pop() {
    if (top == -1) return;
    top--;
}

int main() {
    push(5);
    push(10);
    push(15);
    pop();
    push(20);
    pop();
    pop();
    printf("%d", top);
    return 0;
}
A0
B1
C-1
D2
Attempts:
2 left
💡 Hint
Track each push and pop carefully to update 'top'.
🔧 Debug
advanced
2:00remaining
What error does this stack push function cause?
Identify the error in this push function for a stack implemented using an array of size 3:

void push(int x) { if (top == 3) { printf("Stack Overflow\n"); return; } stack[++top] = x; }

Assume 'top' is initialized to -1.
DSA C
int stack[3];
int top = -1;

void push(int x) {
    if (top == 3) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++top] = x;
}
ASyntax error due to missing semicolon
BNo error, works correctly
CStack underflow error
DStack overflow check misses last valid index, causing out-of-bounds write
Attempts:
2 left
💡 Hint
Array indices go from 0 to size-1.
❓ Predict Output
advanced
2:00remaining
What is the output after these stack operations?
Given a stack of size 4, initially empty, perform:

push(1), push(2), push(3), pop(), push(4), push(5), pop(), pop(), pop()

Print the popped values in order separated by spaces.
DSA C
int stack[4];
int top = -1;

void push(int x) {
    if (top == 3) return;
    stack[++top] = x;
}

int pop() {
    if (top == -1) return -1;
    return stack[top--];
}

int main() {
    push(1);
    push(2);
    push(3);
    printf("%d ", pop());
    push(4);
    push(5);
    printf("%d ", pop());
    printf("%d ", pop());
    printf("%d", pop());
    return 0;
}
A3 5 4 2
B3 4 5 2
C3 4 5 1
D3 5 4 1
Attempts:
2 left
💡 Hint
Remember push fails silently if stack is full.
🧠 Conceptual
expert
2:00remaining
Which option correctly describes the time complexity of stack operations using an array?
Consider a stack implemented using an array with fixed size. What is the time complexity of push and pop operations?
APush is O(n), pop is O(1)
BBoth push and pop have O(1) time complexity
CPush is O(1), pop is O(n)
DBoth push and pop have O(n) time complexity
Attempts:
2 left
💡 Hint
Think about how many elements need to be moved during push or pop.

Practice

(1/5)
1. What is the main purpose of using an array in stack implementation?
easy
A. To store elements in a fixed-size linear structure
B. To allow random access to elements anywhere in the stack
C. To automatically resize the stack when full
D. To sort elements in ascending order

Solution

  1. Step 1: Understand stack storage

    A stack stores elements in a linear order where the last added element is accessed first.
  2. Step 2: Role of array in stack

    An array provides a fixed-size linear structure to hold stack elements in order.
  3. Final Answer:

    To store elements in a fixed-size linear structure -> Option A
  4. Quick Check:

    Array = fixed-size linear storage [OK]
Hint: Stack uses array for fixed-size linear storage [OK]
Common Mistakes:
  • Thinking array allows automatic resizing
  • Confusing stack with random access structures
  • Assuming array sorts elements automatically
2. Which of the following is the correct way to declare a stack array of size 5 in C?
easy
A. int[5] stack;
B. int stack[5];
C. stack int[5];
D. array stack = int[5];

Solution

  1. Step 1: Recall C array declaration syntax

    In C, arrays are declared as: type name[size];
  2. Step 2: Match syntax with options

    int stack[5]; matches correct syntax: int stack[5];
  3. Final Answer:

    int stack[5]; -> Option B
  4. Quick Check:

    C array declaration = int stack[5]; [OK]
Hint: C arrays declared as type name[size]; [OK]
Common Mistakes:
  • Placing type after variable name
  • Using incorrect keywords like 'array'
  • Misplacing brackets in declaration
3. What will be the output after executing the following code?
int stack[3];
int top = -1;
// Push 10
stack[++top] = 10;
// Push 20
stack[++top] = 20;
// Pop
int x = stack[top--];
printf("%d", stack[top]);
medium
A. Runtime error
B. 20
C. 10
D. Garbage value

Solution

  1. Step 1: Trace push operations

    Initially top = -1.
    Push 10: top becomes 0, stack[0] = 10.
    Push 20: top becomes 1, stack[1] = 20.
  2. Step 2: Trace pop and print

    Pop: x = stack[1] = 20, then top becomes 0.
    Print stack[top] = stack[0] = 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    After pop, top points to 10 [OK]
Hint: Pop decreases top, print uses updated top [OK]
Common Mistakes:
  • Printing popped element instead of current top
  • Confusing pre-increment and post-decrement
  • Assuming top points beyond last element
4. Identify the error in this stack push function and select the fix:
void push(int stack[], int *top, int size, int value) {
    if (*top == size) {
        printf("Stack Overflow\n");
        return;
    }
    stack[++(*top)] = value;
}
medium
A. No error, code is correct
B. Change condition to *top == size + 1
C. Change stack[++(*top)] to stack[(*top)++]
D. Change condition to *top == size - 1

Solution

  1. Step 1: Understand top indexing in stack

    Top starts at -1, max index is size - 1.
    When top == size - 1, stack is full.
  2. Step 2: Analyze overflow condition

    Condition *top == size is wrong; it should be *top == size - 1 to detect full stack.
  3. Final Answer:

    Change condition to *top == size - 1 -> Option D
  4. Quick Check:

    Overflow when top reaches size-1 [OK]
Hint: Top max index is size-1, not size [OK]
Common Mistakes:
  • Checking top == size instead of size-1
  • Misusing increment operators in array indexing
  • Ignoring zero-based indexing
5. Given a stack implemented using an array of size 4, initially empty, what will be the stack content after these operations?
push(5);
push(10);
pop();
push(15);
push(20);
pop();
push(25);

Assume top starts at -1 and push/pop update top accordingly. Choose the correct final stack content from bottom to top.
hard
A. 5 -> 15 -> 25 -> null
B. 5 -> 10 -> 15 -> 25 -> null
C. 10 -> 15 -> 25 -> null
D. 5 -> 15 -> 20 -> 25 -> null

Solution

  1. Step 1: Trace each operation step-by-step

    Start: top = -1, stack empty.
    push(5): top=0, stack[0]=5
    push(10): top=1, stack[1]=10
    pop(): removes 10, top=0
    push(15): top=1, stack[1]=15
    push(20): top=2, stack[2]=20
    pop(): removes 20, top=1
    push(25): top=2, stack[2]=25
  2. Step 2: Final stack content

    Stack from bottom (index 0) to top (index 2): 5, 15, 25
  3. Final Answer:

    5 -> 15 -> 25 -> null -> Option A
  4. Quick Check:

    Push/pop update top correctly [OK]
Hint: Track top index after each push/pop [OK]
Common Mistakes:
  • Forgetting to update top after pop
  • Assuming pop removes bottom element
  • Mixing order of elements after operations
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard