B - Card Pile
Source: atcoder
Difficulty: 200

Problem Statement
There is a stack of
100
cards, each labeled with the integer
0
.
Process
Q
queries. Each query is of one of the following:
Type
1
: Place a card labeled with an integer
x
on top of the stack.
Type
2
: Remove the top card of the stack and output the integer written on that removed card. Under the constraints of this problem, the stack always has at least one card.
Constraints
1 \le Q \le 100
1 \le x \le 100
There is at least one query of type
2
.
All input values are integers.
Input
The input is given from Standard Input in the following format:
Q
\text{query}_1
\text{query}_2
\vdots
\text{query}_Q
The
i
-th query
\text{query}_i
starts with the query type
c_i
(
1
or
2
), followed by the integer
x
if
c_i=1
.
That is, each query is in one of the following two formats:
1
x
2
Output
Let
q
be the number of queries with
c_i=2
. Print
q
lines.
The
j
-th line (
1 \le j \le q
) should contain the answer to the
j
-th such query.
Sample Input 1
6
2
1 4
1 3
2
2
2
Sample Output 1
0
3
4
0
After processing each query, the stack is as follows:
Remove the top card of the stack. The integer on the removed card is
0
, so output
0
.
The stack then has
99
cards labeled with
0
.
Add a card labeled
4
on top.
The stack then has
1
card labeled
4
, and
99
cards labeled
0
, from top to bottom.
Add a card labeled
3
on top.
The stack then has
1
card labeled
3
,
1
card labeled
4
, and
99
cards labeled
0
, from top to bottom.
Remove the top card. The integer on that card is
3
, so output
3
.
The stack then has
1
card labeled
4
, and
99
cards labeled
0
, from top to bottom.
Remove the top card. The integer on that card is
4
, so output
4
.
The stack then has
99
cards labeled
0
.
Remove the top card. The integer on that card is
0
, so output
0
.
The stack then has
98
cards labeled
0
.
Sample Input 2
5
2
2
2
2
2
Sample Output 2
0
0
0
0
0

C++ solution:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
map<ll , ll>mp;
void afdy()
{
    stack<int>stk;
    int q;cin >> q;
    while(q--)
    {
        int op;cin >> op;
        if(op == 1)
        {
            int x;cin >> x;
            stk.push(x);
        }
        else if(op == 2)
        {
            if(stk.empty()) cout << 0 << '\n';
            else
            {
                cout << stk.top() << '\n';
                stk.pop();
            }
        }
    }
}
int main()
{
    ios::sync_with_stdio(0) , cin.tie(0) , cout.tie(0);
    int _ = 1;
    // cin >> _;
    while(_--) afdy();
    return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"
)

var (
	sc = bufio.NewScanner(os.Stdin)
	wt = bufio.NewWriter(os.Stdout)
)

const (
	inf = 1 << 60
	MOD = 998244353
)

func main() {
	/*** INIT ***/
	defer wt.Flush()
	sc.Buffer([]byte{}, inf)
	sc.Split(bufio.ScanWords)

	q := InputInt()
	que := make([]int, 0, 200)
	for i := 0; i < 100; i++ {
		que = append(que, 0)
	}
	for 0 < q {
		q--
		op := InputInt()
		if op == 1 {
			x := InputInt()
			que = append(que, x)
		} else {
			OutInt(que[len(que)-1])
			que = que[:len(que)-1]
		}
	}
}

/*** I/O ***/
func InputInt() int {
	sc.Scan()
	val, _ := strconv.Atoi(sc.Text())
	return val
}

func InputString() string {
	sc.Scan()
	return sc.Text()
}

func OutInt(val int) {
	fmt.Fprintln(wt, val)
}

func OutString(s string) {
	fmt.Fprintln(wt, s)
}

Java solution:
import java.io.*;
import java.util.*;

public class Main {
	private static int q;
	private static Deque<Integer> dq = new ArrayDeque<>();
	private static StringBuilder sb = new StringBuilder();

	public static void main(String[] args) throws IOException {
		BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));

		q = Integer.parseInt(bf.readLine());
		while(q-- > 0) {
			StringTokenizer st = new StringTokenizer(bf.readLine());
			int cmd = Integer.parseInt(st.nextToken());
			if(cmd == 1) {
				int x = Integer.parseInt(st.nextToken());
				dq.addFirst(x);
			}
			else {
				if(dq.isEmpty()) sb.append(0);
				else sb.append(dq.removeFirst());
				sb.append("\n");
			}
		}

		System.out.println(sb);
	}
}

JavaScript solution:
// AtCoder Boilerplate for Node.js 18.16.1
process.stdin.resume();
process.stdin.setEncoding('utf8');

let inputString = '';
let currentLine = 0;

process.stdin.on('data', (inputStdin) => {
    inputString += inputStdin;
});

process.stdin.on('end', () => {
    inputString = inputString.trim().split('\n');
    main();
});

function readLine() {
    return inputString[currentLine++];
}

// Utility functions for parsing input
function readInt() {
    return parseInt(readLine());
}

function readInts() {
    return readLine().split(' ').map(Number);
}

function readIntMatrix(n) {
    let matrix = [];
    for (let i = 0; i < n; i++) {
        matrix.push(readInts());
    }
    return matrix;
}

function readStrings() {
    return readLine().split(' ');
}

function readString() {
    return readLine();
}

// Main function to handle the problem
function main() {
    // Read the number of queries
    let Q = readInt();
    let queries = [];

    // Store the original Q value
    const totalQueries = Q;
    
    while(Q--){
        let query = readInts();
        if(query[0]===1){
            queries.push([query[0], query[1]]);
        } else{
            queries.push([query[0]]);
        }
    }
    
    const result = solve(queries);
    // Join the array elements with newlines for proper formatting
    console.log(result.join('\n'));
}

/**
 * Your solution function - implement your logic here
 * @param {number} n - The first parameter (example: length of array)
 * @param {number[]} arr - The second parameter (example: array of integers)
 * @returns {string|number} - The result to be printed
 */
function solve(queries) {
    let answer = [];
    let stackArray = [];

    for(let i=0; i<queries.length; i++){
        if(queries[i][0] === 1) {
            stackArray.push(queries[i][1]);
        } else {
            // Check if stack is not empty before popping
            if(stackArray.length > 0) {
                answer.push(stackArray[stackArray.length-1]);
                stackArray.pop();
            } else {
                // Handle empty stack case
                answer.push(0);
            }
        }
    }
    return answer;
}

Python solution:
q=int(input())
que=[]
for i in range(q):
    query = list(map(int, input().split()))
    if query[0] == 1:
        que.append(query[1])
    elif query[0] == 2:
        if len(que) == 0:
            print(0)
        else:    
            print(que.pop(len(que)-1))

Ruby solution:
Q = gets.to_i
query = Q.times.map { gets.split.map(&:to_i) }
extra = []
query.each do |q|
  case q[0]
  when 2
    if extra.empty?
      puts 0
    else
      puts extra.pop
    end
  when 1
    extra << q[1]
  end
end