Author: admin

  • Master UART USART Interview Questions (Beginner Friendly 2026)

    1. Introduction to UART and USART

    UART USART Interview Questions : Understand the difference between UART (Universal Asynchronous Receiver Transmitter) and USART (Universal Synchronous/Asynchronous Receiver Transmitter). You’ll learn that both are serial communication protocols, but USART can work in both asynchronous and synchronous modes, while UART works only in asynchronous mode.

    Understand the Importance of Serial Communication: You will grasp why UART and USART are widely used for communication between microcontrollers and other devices like sensors, actuators, and peripherals, making it essential for embedded systems design.

    2. Fundamental Components of UART and USART

    • Start, Data, and Stop Bits: You will learn about the data frame structure in UART/USART communication, which includes start bits, data bits, and stop bits. You’ll gain knowledge of how data is transmitted serially, one bit at a time.
    • Baud Rate, Parity, and Flow Control: Understand key communication parameters like baud rate, parity bit, and flow control mechanisms such as RTS/CTS (hardware flow control) and XON/XOFF (software flow control).
      • Baud Rate determines the speed of communication.
      • Parity provides a method to detect errors in data transmission.
      • Flow control prevents data loss by ensuring that the receiving end is ready to accept more data.

    3. Data Transmission in UART/USART

    • Transmission Modes – Asynchronous vs. Synchronous: Learn the core difference between asynchronous communication (in UART) and synchronous communication (in USART) and when each mode is used.
      • In asynchronous mode, there is no clock signal. Data is sent using start and stop bits.
      • In synchronous mode, there is a shared clock signal, which allows for faster communication.
    • Learn About Full-Duplex and Half-Duplex: You’ll understand how full-duplex communication (where data can be transmitted and received simultaneously) works, as well as half-duplex (where data flows in only one direction at a time).

    4. Configuring UART and USART Communication

    • Pin Configurations (TX/RX, RTS/CTS): Learn how to configure the essential pins of UART/USART like TX (Transmit) and RX (Receive) for basic communication. You’ll also learn about RTS and CTS pins used for hardware flow control.
    • Baud Rate Configuration: Learn how to configure the baud rate for accurate data transmission between two devices. You will understand how baud rates are calculated based on system clock settings.
    • Parity and Stop Bits: Learn how to set parity (even, odd, none) and configure the number of stop bits to ensure correct data transmission and error detection.

    5. Error Handling in UART/USART

    • Framing Errors: Learn what framing errors are and how they occur when the receiver doesn’t receive the correct number of bits for each data frame.
    • Overrun Errors: Understand overrun errors that happen when the receiver’s buffer is full, and new data arrives before the previous data is processed.
    • Parity Errors: Learn how parity errors occur when the parity bit doesn’t match the expected value, signaling corrupted data.

    6. Practical Applications of UART and USART

    • Communication Between Microcontrollers: Learn how to use UART/USART for communication between microcontrollers in a system.
    • Interfacing with Peripherals: Gain knowledge of how UART is commonly used to interface with peripherals like GPS modules, Bluetooth modules, and other devices for wireless communication, debugging, and more.
    • Debugging Embedded Systems via UART: Discover how serial debugging works, where UART is used to send debug messages from microcontrollers to a terminal for monitoring and troubleshooting.

    7. Practical Considerations for UART/USART Implementation

    • Voltage Level Considerations: Learn about the voltage levels required for UART communication (e.g., 3.3V or 5V), and understand the importance of ensuring compatible voltage levels between devices or using level shifters.
    • Cable Length and Transmission Distance: Understand the limitations of UART communication, such as transmission distance, and the use of buffer amplifiers or signal conditioning for longer cable runs.
    • Setting Up a UART Communication Link: You will learn how to physically wire up UART links, configure microcontroller peripherals for UART operation, and troubleshoot issues like incorrect wiring, mismatched baud rates, or pin configurations.

    8. Hands-On Learning

    • Writing Code to Initialize UART/USART: Learn how to write embedded code to initialize UART/USART on a microcontroller, configure parameters like baud rate, data bits, and flow control, and set up an interrupt-based UART communication system.
    • Sending and Receiving Data: Gain hands-on experience with sending and receiving data via UART using software libraries, handling the data in your code, and ensuring reliable communication.
    • Implementing Flow Control in UART Communication: You will learn how to implement hardware or software flow control to prevent data loss in high-speed communication scenarios.

    9. Debugging and Troubleshooting UART/USART

    • Use of Serial Monitors and Logic Analyzers: Learn how to use tools like serial monitors to visualize data sent over UART. You’ll also discover how to use logic analyzers to troubleshoot UART/USART signals by capturing and decoding the data stream.
    • Testing Communication Between Devices: Learn how to test the UART communication link between two devices, identify common problems like baud rate mismatches, incorrect wiring, or noise in the data transmission.

    What Skills You Will Gain:

    • UART/USART Protocols: A strong understanding of both asynchronous (UART) and synchronous (USART) communication, including how to configure them on embedded devices.
    • Error Handling: Practical knowledge of handling communication errors like framing, overrun, and parity errors.
    • Flow Control Management: How to implement flow control techniques (RTS/CTS and XON/XOFF) to ensure reliable communication in high-speed applications.
    • Embedded Programming Skills: Learn how to write embedded code for configuring, sending, and receiving data over UART/USART on microcontrollers and other devices.
    • Practical Troubleshooting: Master debugging techniques and use of tools like serial monitors and logic analyzers for troubleshooting UART/USART communication.

    Learning Outcomes

    By the end of your learning journey on UART and USART communication, you will:

    1. Be able to configure UART/USART on embedded devices and interfaces.
    2. Understand how data frames are structured and how transmission occurs bit by bit.
    3. Be equipped with the skills to handle baud rate, flow control, and error management.
    4. Gain hands-on experience in sending and receiving data in real-world embedded systems.
    5. Learn to troubleshoot and debug UART communication using various tools and techniques.
    6. Be proficient in implementing UART communication between multiple devices, including microcontrollers and peripherals.

    What is UART/USART?

    • UART stands for Universal Asynchronous Receiver Transmitter.
    • USART stands for Universal Synchronous/Asynchronous Receiver Transmitter.

    💡 Simple difference:

    • UART = Only Asynchronous communication.
    • USART = Can do both Synchronous and Asynchronous.

    Basic Interview Questions

    1. What is UART?

    UART is a hardware communication protocol used to send and receive data serially (bit-by-bit) over two lines:

    • TX (Transmit)
    • RX (Receive)

    It does not require a clock signal and is hence asynchronous.

    2. What is the difference between UART and USART?

    FeatureUARTUSART
    ClockNo clockCan use clock (synchronous)
    ModeAsynchronousSynchronous & Asynchronous
    SpeedSlowerFaster in synchronous mode

    3. What are the main pins in UART?

    • TX (Transmit) – sends data
    • RX (Receive) – receives data
      Optional:
    • GND – common ground between devices

    4. How does UART communication work?

    When UART sends data:

    • It adds a Start Bit (0)
    • Sends data bits (typically 8)
    • Adds an optional Parity Bit
    • Ends with one or more Stop Bits (1)

    💡 Example Frame:
    [Start][D0][D1][D2]...[D7][Parity][Stop]

    5. What is baud rate?

    • Baud rate is the speed of communication.
    • It defines the number of bits sent per second (bps).
    • Common values: 9600, 115200, etc.

    Example: 9600 baud = 9600 bits per second.

    6. Is UART full-duplex or half-duplex?

    • Full-duplex.
    • Can send and receive data simultaneously using separate TX and RX lines.

    7. What is the difference between SPI, I2C, and UART?

    FeatureUARTSPII2C
    Wires2 (TX, RX)4 (MOSI, MISO, SCK, SS)2 (SDA, SCL)
    SpeedMediumFastSlower
    Master-SlavePeer-to-peerMaster-SlaveMaster-Slave
    ComplexityLowMediumHigh

    8. What is a parity bit in UART?

    • Used for error detection.
    • Even parity: number of 1s including the parity bit is even.
    • Odd parity: number of 1s including the parity bit is odd.

    9. What happens if baud rates don’t match?

    If two devices use different baud rates:

    • Data will be misaligned.
    • Receiver may see garbage data or framing errors.

    10. How can you implement UART in embedded systems?

    Two common ways:

    1. Using built-in UART hardware module (e.g., STM32, Atmega)
    2. Software UART (bit-banging) – manually control GPIOs using code (less reliable)

    Bonus: Code Example (Arduino)

    void setup() {
      Serial.begin(9600); // Set baud rate
    }
    
    void loop() {
      Serial.println("Hello UART!");
      delay(1000);
    }
    

    Quick Tips for Interviews:

    • Know the UART data frame structure.
    • Understand baud rate and error handling.
    • Be ready to explain how UART differs from SPI/I2C.
    • Practice writing simple UART send/receive code.

    Basic Questions:

    1. What is UART?
    2. What is USART, and how is it different from UART?
    3. Explain the basic working principle of UART communication.
    4. What are the key differences between synchronous and asynchronous communication?
    5. What are the main pins used in UART communication?
    6. What is the baud rate in UART communication?
    7. What is the significance of the start bit and stop bit in UART communication?
    8. What are data bits in UART?
    9. What is a parity bit, and what are the types of parity used in UART?
    10. What are the advantages of UART over other serial communication protocols (e.g., SPI, I2C)?

    Intermediate Questions:

    1. What is full-duplex communication in UART?
    2. Explain the role of the TX and RX lines in UART.
    3. What happens if the baud rates of the sender and receiver do not match in UART communication?
    4. What is the maximum distance supported by UART communication?
    5. Explain the error detection mechanisms in UART (e.g., framing error, overrun error).
    6. How does UART handle data flow control? What are the methods of flow control?
    7. Explain how the receiver knows when to start reading data in UART.
    8. Can UART communication be used for long-range communication? Why or why not?
    9. What is the purpose of the flow control signals (RTS/CTS) in UART?
    10. What are the key differences between SPI, I2C, and UART?

    Advanced Questions:

    1. What is the significance of the baud rate, and how is it determined?
    2. What are the various error types in UART communication, and how can they be mitigated?
    3. How does UART handle synchronization between the transmitter and receiver?
    4. What is the role of the framing error in UART communication?
    5. Explain how to implement UART communication using interrupts in embedded systems.
    6. How can you implement a UART-based protocol for communication between multiple devices?
    7. What are the typical applications of UART in embedded systems?
    8. How can UART be used in embedded systems to communicate with peripheral devices (e.g., sensors, Bluetooth modules)?
    9. What is a “half-duplex” mode in UART, and how does it differ from full-duplex?
    10. Explain how UART data is transmitted and received in the form of binary frames.

    Practical/Implementation-Based Questions:

    1. Write a simple code to transmit data using UART in Arduino.
    2. How would you troubleshoot a UART communication problem where data is not being received properly?
    3. How would you handle UART communication errors (e.g., framing errors or overrun errors) in embedded systems code?
    4. What would you do if you have a mismatch in the baud rate or data format between two UART devices?
    5. Explain how you would implement UART communication in an RTOS environment (e.g., QNX or FreeRTOS).
    6. Design a UART-based communication protocol between a microcontroller and a PC.
    7. How would you perform UART-based data logging in embedded systems?

    Behavioral/Conceptual Questions:

    1. Why do you think UART is still widely used in embedded systems?
    2. What are the challenges you face while working with UART communication in embedded systems?
    3. Explain the trade-offs between UART and other serial communication protocols like SPI or I2C.
    4. Can you explain a scenario where you had to debug UART communication issues in your previous projects?
    5. What are the key considerations when selecting UART for a new embedded system design?

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on Embedded Prep

  • Privacy Policy

    Effective Date: June 21, 2025
    App Name: SSC GK MCQ in Hindi 2025
    Developer: Exam Guide

    1. Introduction

    This privacy policy explains how SSC GK MCQ in Hindi 2025 (“we”, “our”, or “us”) handles user data and protects your privacy while using our app. By using the app, you agree to the terms of this privacy policy.

    2. Information We Do Not Collect

    We do not collect, store, or share any personal information such as:

    • Your name, email address, or contact details
    • Your device ID or IP address
    • Your location or browsing history

    This app is designed for educational purposes and works completely offline (if true). It does not require any login or registration.

    3. Permissions

    Our app may request standard permissions (such as internet access) only to:

    • Load web-based content (e.g., if there are any WebView quiz features)
    • Show ads (if AdMob or other ad services are integrated)

    We do not use permissions to access any sensitive user data.

    4. Third-Party Services

    If our app displays advertisements or analytics, third-party services like Google AdMob or Google Analytics for Firebase may collect limited non-personal data such as:

    • Anonymous device information
    • Ad performance statistics

    5. Children’s Privacy

    Our app is suitable for users of all ages and does not knowingly collect personal information from children under the age of 13.

    6. Changes to This Privacy Policy

    We may update this privacy policy from time to time. We encourage you to review this page periodically for any changes. The updated policy will be effective as of the date it is posted.

    7. Contact Us

    If you have any questions or suggestions about this Privacy Policy, please contact us:

    Email: sscgkmcqapp@gmail.com

  • Object Detection Using Raspberry Pi, HC-SR04, LED, and Buzzer | Master Implementation using python 2026

    Object Detection Using Raspberry Pi : In this project, we’ll build a simple object detection system using a Raspberry Pi. When the ultrasonic sensor detects an object within a certain distance (e.g., less than 10 cm), it will turn on an LED and sound a buzzer.

    Perfect for beginners, this project combines basic GPIO usage with real-world hardware.

    Components Required for Object Detection Using Raspberry Pi

    ComponentQuantity
    Raspberry Pi (any model with GPIO)1
    HC-SR04 Ultrasonic Sensor1
    Breadboard1
    Jumper Wires~10
    LED1
    Resistor (220Ω for LED)1
    Active Buzzer1
    Optional: Resistors for voltage divider (1kΩ + 2kΩ)2

    Raspberry Pi GPIO Pin Mapping (BCM Mode)

    PurposeGPIO PinPhysical PinComponent Pin
    Trigger (Ultrasonic)GPIO 23Pin 16HC-SR04 TRIG
    Echo (Ultrasonic)GPIO 24Pin 18HC-SR04 ECHO
    LED ControlGPIO 18Pin 12LED (via resistor)
    Buzzer ControlGPIO 25Pin 22Buzzer (+ve)
    5V Power5VPin 2 or 4HC-SR04 VCC, Buzzer
    GroundGNDPin 6 or 9All GND pins

    Wiring Instructions

    1. HC-SR04 Ultrasonic Sensor

    HC-SR04 PinConnect To
    VCC5V (Pin 2/4)
    GNDGND (Pin 6)
    TRIGGPIO 23 (Pin 16)
    ECHOGPIO 24 via voltage divider

    ⚠️ Important: The ECHO pin outputs 5V, but Raspberry Pi GPIO only supports 3.3V. Use a voltage divider:

    • 1kΩ resistor between Echo and Pi GPIO24
    • 2kΩ resistor between Echo and GND
    • Tap from junction to GPIO24

    2. LED

    LED PinConnect To
    Anode (+)GPIO 18 via 220Ω resistor
    Cathode (-)GND

    3. Buzzer (Active Type)

    Buzzer PinConnect To
    +veGPIO 25
    -veGND

    If you’re using a passive buzzer, use a transistor (NPN) to drive it safely.

    Python Code

    Here’s the complete Python script to read the ultrasonic sensor, and control the LED and buzzer based on object detection:

    import RPi.GPIO as GPIO
    import time
    
    # Set GPIO mode
    GPIO.setmode(GPIO.BCM)
    
    # Define pin numbers
    GPIO_TRIGGER = 23
    GPIO_ECHO = 24
    LED_PIN = 18
    BUZZER_PIN = 25
    
    # Set pin directions
    GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
    GPIO.setup(GPIO_ECHO, GPIO.IN)
    GPIO.setup(LED_PIN, GPIO.OUT)
    GPIO.setup(BUZZER_PIN, GPIO.OUT)
    
    def distance():
        # Send 10us pulse to trigger
        GPIO.output(GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER, False)
    
        start_time = time.time()
        stop_time = time.time()
    
        while GPIO.input(GPIO_ECHO) == 0:
            start_time = time.time()
    
        while GPIO.input(GPIO_ECHO) == 1:
            stop_time = time.time()
    
        time_elapsed = stop_time - start_time
        distance_cm = (time_elapsed * 34300) / 2
        return distance_cm
    
    try:
        while True:
            dist = distance()
            print(f"Measured Distance = {dist:.1f} cm")
    
            if dist < 10.0:
                GPIO.output(LED_PIN, GPIO.HIGH)
                GPIO.output(BUZZER_PIN, GPIO.HIGH)
            else:
                GPIO.output(LED_PIN, GPIO.LOW)
                GPIO.output(BUZZER_PIN, GPIO.LOW)
    
            time.sleep(1)
    
    except KeyboardInterrupt:
        print("Measurement stopped by User")
        GPIO.cleanup()
    

    How It Works

    • The HC-SR04 sends an ultrasonic pulse.
    • When it bounces back from an object, the sensor calculates the distance.
    • If the distance is less than 10 cm, it:
      • Turns ON the LED
      • Activates the buzzer
    • Otherwise, both remain OFF.

    Tips

    • Ensure all ground connections are common.
    • Always power off Raspberry Pi while wiring.
    • Use GPIO.cleanup() to reset pins after the script ends.
    • You can tweak the threshold distance (if dist < 10.0:) based on your requirement.

    Applications

    • Obstacle detection for robots
    • Proximity-based alerts
    • Contactless doorbell or switch
    • Smart trash bin lid opener

    You can expand it further by adding a display (OLED/LCD), sending alerts via email, or connecting to a mobile app!

    UltrasonicMailCam.py
    import RPi.GPIO as GPIO
    import time
    import subprocess
    import yagmail
    import os
    
    # === GPIO Pin Setup ===
    GPIO.setmode(GPIO.BCM)
    
    GPIO_TRIGGER = 23
    GPIO_ECHO = 24
    LED_PIN = 18
    BUZZER_PIN = 25
    
    GPIO.setup(GPIO_TRIGGER, GPIO.OUT)
    GPIO.setup(GPIO_ECHO, GPIO.IN)
    GPIO.setup(LED_PIN, GPIO.OUT)
    GPIO.setup(BUZZER_PIN, GPIO.OUT)
    
    # === Distance Measurement Function ===
    def measure_distance():
        GPIO.output(GPIO_TRIGGER, True)
        time.sleep(0.00001)
        GPIO.output(GPIO_TRIGGER, False)
    
        start_time = time.time()
        stop_time = time.time()
    
        while GPIO.input(GPIO_ECHO) == 0:
            start_time = time.time()
        while GPIO.input(GPIO_ECHO) == 1:
            stop_time = time.time()
    
        time_elapsed = stop_time - start_time
        distance_cm = (time_elapsed * 34300) / 2
        return distance_cm
    
    # === Image Capture Function ===
    def capture_image_libcamera(filename="captured.jpg"):
        subprocess.run(["libcamera-still", "-o", filename, "--nopreview"], check=True)
    
    # === Email Sending Function ===
    def send_email_with_attachment(sender, app_password, receiver, subject, message, attachment_path):
        yag = yagmail.SMTP(user=sender, password=app_password)
        yag.send(
            to=receiver,
            subject=subject,
            contents=message,
            attachments=attachment_path
        )
        print("📧 Email sent successfully!")
    
    # === Main Function ===
    if __name__ == '__main__':
        try:
            image_sent = False  # To avoid sending multiple emails rapidly
    
            while True:
                dist = measure_distance()
                print(f"Measured Distance = {dist:.1f} cm")
    
                if dist < 10.0:
                    GPIO.output(LED_PIN, GPIO.HIGH)
                    GPIO.output(BUZZER_PIN, GPIO.HIGH)
    
                    if not image_sent:
                        print("📸 Object detected! Capturing image and sending email...")
    
                        # Capture image
                        image_file = "captured.jpg"
                        capture_image_libcamera(image_file)
    
                        # Send email
                        sender_email = "nishantsingh2jan1998@gmail.com"
                        app_password = "rooh lcrd byqw wuab"
                        receiver_email = "nishantkumarsingh131@gmail.com"
                        subject = "Captured Image from Raspberry Pi"
                        message = "Hi, this is the captured image attached."
                        send_email_with_attachment(sender_email, app_password, receiver_email, subject, message, image_file)
    
                        # Remove image after sending
                        if os.path.exists(image_file):
                            os.remove(image_file)
                            print("🗑️ Temporary image file removed.")
    
                        image_sent = True  # Mark as sent
    
                else:
                    GPIO.output(LED_PIN, GPIO.LOW)
                    GPIO.output(BUZZER_PIN, GPIO.LOW)
                    image_sent = False  # Reset flag when object is gone
    
                time.sleep(1)
    
        except KeyboardInterrupt:
            print("\n🛑 Program stopped by user.")
            GPIO.cleanup()
    

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on

  • Next Greater Element in Array : Beginner Friendly Explanation with Example | Leetcode Solution (2026)

    If you’re preparing for coding interviews or practicing data structure problems, chances are you’ll encounter the Next Greater Element in array problem. It’s one of the most common and important questions related to arrays and stacks — frequently asked by top tech companies.

    In this beginner-friendly tutorial, we’ll explore the concept of Next Greater Element in an array with simple logic, step-by-step explanation, dry run examples, and multiple code implementations (brute-force and stack-based).

    Whether you’re a student, a beginner programmer, or an aspiring software developer, this guide will help you build a strong foundation in solving array problems using stacks..

    What is the Next Greater Element?

    The Next Greater Element in array is a classic coding interview problem that tests your understanding of arrays and stack-based logic.

    Here’s the problem statement:

    Given an array of integers, your task is to find the next greater element for each element in the array.
    The next greater element for an element x is the first element to the right of x that is greater than x.

    If no greater element exists on the right side, the answer is -1 for that position.

    🔍 Example:

    Input:  [4, 5, 2, 25]
    Output: [5, 25, 25, -1]
    

    Explanation:

    • Next greater of 4 is 5
    • Next greater of 5 is 25
    • Next greater of 2 is 25
    • 25 has no greater element, so answer is -1

    This concept is commonly used in problems involving stock span, daily temperatures, and monotonic stacks. Understanding the Next Greater Element in array is a stepping stone to solving more advanced array-based questions.

    Brute Force Approach to Find Next Greater Element in Array (Easy to Understand)

    ✅ Logic:

    In this basic approach to solving the Next Greater Element in array, we use two nested loops. For each element in the array, we scan all elements to its right and find the first greater element.

    If we don’t find any, we simply put -1.

    🔁 Time Complexity:

    • O(n²) – Because for each element, we loop through the remaining elements on its right.

    This method is easy to understand but not efficient for large input sizes.

    🧑‍💻 C++ Code: Brute Force for Next Greater Element

    #include <iostream>
    #include <vector>
    using namespace std;
    
    void printNGE(vector<int>& arr) {
        int n = arr.size();
        for(int i = 0; i < n; i++) {
            int next = -1;
            for(int j = i + 1; j < n; j++) {
                if(arr[j] > arr[i]) {
                    next = arr[j];
                    break;
                }
            }
            cout << arr[i] << " --> " << next << endl;
        }
    }
    
    int main() {
        vector<int> arr = {4, 5, 2, 25};
        printNGE(arr);
        return 0;
    }
    

    Efficient Stack-Based Approach to Next Greater Element in Array (O(n) Time)

    When solving the Next Greater Element in array, an efficient solution is to use a stack. This reduces the time complexity from O(n²) to O(n) and is widely asked in coding interviews.

    🧠 Logic:

    We process the array from right to left, using a stack to keep track of potential next greater candidates.

    📚 Step-by-Step Dry Run:

    For input array: [4, 5, 2, 25]

    1. Start from the rightmost element.
    2. For each element:
      • Remove all elements from the stack smaller than or equal to it.
      • If the stack becomes empty, the next greater element is -1.
      • Otherwise, the top of the stack is the next greater.
    3. Push the current element onto the stack for future comparisons.

    🧑‍💻 C++ Code: Stack-Based Next Greater Element

    #include <iostream>
    #include <vector>
    #include <stack>
    using namespace std;
    
    vector<int> nextGreaterElement(vector<int>& arr) {
        int n = arr.size();
        vector<int> result(n, -1);
        stack<int> st;
    
        for(int i = n - 1; i >= 0; i--) {
            // Pop elements smaller than or equal to arr[i]
            while(!st.empty() && st.top() <= arr[i]) {
                st.pop();
            }
    
            // If stack is not empty, top is the next greater element
            if(!st.empty()) {
                result[i] = st.top();
            }
    
            // Push current element
            st.push(arr[i]);
        }
    
        return result;
    }
    
    int main() {
        vector<int> arr = {4, 5, 2, 25};
        vector<int> res = nextGreaterElement(arr);
    
        for(int i = 0; i < arr.size(); i++) {
            cout << arr[i] << " --> " << res[i] << endl;
        }
    
        return 0;
    }
    

    🎯 Output:

    4 --> 5  
    5 --> 25  
    2 --> 25  
    25 --> -1
    

    Why Stack is Useful for Next Greater Element Problems?

    Using a stack makes this problem highly efficient because it stores elements in a last-in, first-out (LIFO) structure, allowing us to track potential next greater elements without scanning the whole array repeatedly.

    This stack-based method is especially powerful for large datasets and is frequently used in:

    • Stock span problems
    • Temperature prediction arrays
    • Monotonic stack patterns

    Time & Space Complexity Summary

    ApproachTime ComplexitySpace Complexity
    Brute ForceO(n²)O(1)
    Stack-based MethodO(n)O(n)

    More Problems Related to Next Greater Element in Array

    Once you’re comfortable with the concept, try solving these related variations:

    • ✅ Next Smaller Element in Array
    • ✅ Next Greater Element in Circular Array
    • ✅ Previous Greater Element in Array
    • ✅ Daily Temperatures Problem (LeetCode)
    • ✅ Stock Span Problem

    Final Words

    The Next Greater Element in array is a foundational algorithm that helps build problem-solving skills involving arrays, stacks, and greedy logic.

    If you understand both the brute force and optimized approaches, you’ll be able to solve a wide range of real-world problems that follow a similar pattern.

    Keep practicing and exploring new variations — and remember: every great coder once started with basics like this!

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on EmbeddedPrep

  • Stack Data Structure | Complete Beginner’s Guide (2026)

    Welcome to the complete guide on mastering Stack Data Structure
    Whether you’re a beginner just learning data structures, preparing for interviews, or brushing up on recursion, this page will guide you through one of the most fundamental and powerful data structures.

    In this tutorial series, you’ll not only learn how stacks work but also solve real-world problems using recursion, arrays, and linked lists.

    What is a Stack?

    A stack is a linear data structure that follows the Last In First Out (LIFO) principle — the last item added is the first one removed.
    Imagine a stack of plates: you add to the top and remove from the top. 🥞

    In programming, stacks are used in:

    • Expression evaluation and syntax parsing
    • Undo operations in editors
    • Function call management (call stack)
    • Backtracking problems
    • Balanced parentheses checking
    • Depth-first search algorithms

    Tutorials You’ll Find in This Series

    TopicWhat You’ll Learn
    ✅ Stack Implementation using ArrayLearn how to create a basic stack using arrays. Understand operations like push, pop, peek, and check if it’s empty. Great for C++ and C beginners.
    ✅ 2 Stacks in One ArrayImplement two independent stacks in the same array to save memory — a commonly asked space-optimization problem.
    ✅ Delete Middle Element of StackPractice recursion by deleting the middle element of a stack without using any extra data structure.
    ✅ Valid Parentheses ProblemUse stacks to validate expressions like {[()]}. This problem builds logic and appears in almost every coding test.
    ✅ Insert Element at Bottom of StackDive into recursion by inserting an element at the bottom of the stack — an essential sub-step for reversing stacks.
    ✅ Reverse a Stack Using RecursionReverse the stack without any extra space using recursion — builds a strong grasp of recursive thinking.
    ✅ Sort a Stack Using RecursionSort stack elements in ascending order using only recursive calls. A true test of your logic-building skills.
    ✅ Redundant BracketsCheck if an expression has unnecessary or redundant brackets like "((a+b))". Frequently asked by top tech companies.
    ✅ Minimum Bracket ReversalGiven a string with only { and }, find the minimum reversals required to balance the expression. Learn how to count and pair brackets using a stack.

    Why This Series is Perfect for Beginners

    • 📌 Each topic starts from scratch with clear concepts.
    • 💡 Includes dry runs, so you see how the stack changes step by step.
    • 💻 Uses simple C++ code with detailed explanation.
    • 🧪 Comes with practice problems to test your understanding.
    • 📊 Some tutorials use diagrams to help visualize stack operations.
    • 🚀 Recursion + Stack combo is perfect for strengthening logical thinking.

    Build Your Foundations Strong

    Mastering stacks is like building a strong base for competitive programming and interviews. Once you get confident with stacks, you’ll find it easier to solve:

    • Backtracking problems (e.g., Rat in a Maze, Sudoku Solver)
    • Expression Conversion (Infix to Postfix)
    • Tree traversals (postorder, inorder)
    • Recursion-based problems like Tower of Hanoi
    • Compiler syntax checks

    Extra Practice Ideas

    To make your learning journey even more interactive, try solving these after each tutorial:

    • 🔄 Convert each recursive solution into an iterative one using a custom stack.
    • ✍️ Write your own input/output examples and dry-run on paper.
    • 💬 Try explaining the stack changes out loud or to a friend.
    • 🎨 Draw the call stack if recursion is confusing — visuals help!
    • 🧠 Combine multiple operations, like reversing and then sorting a stack.

    Final Words

    Stacks are the building blocks of logical problem solving. With each tutorial, you’re getting closer to mastering data structures and becoming an efficient coder.

    So, what are you waiting for? 🚀
    Dive into each topic listed above — whether you’re sorting, reversing, or validating brackets — and let’s master stacks together!

    Happy Coding! 💻✨
    Let’s stack up your skills — one pop and push at a time!

  • Sorting a Stack Using Recursion in C++ | Master Beginner-Friendly Guide 2026

    Sorting a Stack Using Recursion : In this tutorial, we will learn how to sort a stack of numbers using recursion in C++. A stack is a special data structure where you can only add or remove elements from the top, just like a stack of plates. Sorting a stack might seem tricky because you cannot access elements in the middle directly.

    We will use recursion — a technique where a function calls itself — to break down the problem into smaller parts. First, we remove elements from the stack one by one until it’s empty. Then, we insert each removed element back into the stack in the correct sorted position.

    This method does not use any extra data structures, and it helps you understand both recursion and stack operations deeply. The final result is a stack sorted in ascending order, with the smallest element at the bottom and the largest at the top.

    The article also includes a step-by-step dry run to help beginners understand exactly how the stack changes during sorting.

    What is a Stack?

    A stack is a data structure that works like a pile of plates. You can only add or remove the top plate. This is called LIFO (Last In, First Out).

    • You push to add an item on the top.
    • You pop to remove the item from the top.
    • You can check the top element anytime.

    What Does Sorting a Stack Using Recursion program Do?

    The program sorts the elements in a stack so that the smallest number is at the bottom and the largest number is at the top — all using recursion (functions calling themselves).

    Code Breakdown of Sorting a Stack Using Recursion

    #include <iostream>
    #include <stack>
    using namespace std;
    
    • We include necessary libraries.
    • stack helps us use stack data structure.
    • iostream allows input/output.

    Two Important Functions

    1. sortedInsert — Inserts a number into the correct position in an already sorted stack.
    2. sortStack — Sorts the entire stack using recursion.

    Function: sortedInsert

    void sortedInsert(stack<int> &st, int x) {
        if (st.empty() || st.top() > x) {
            st.push(x);
            return;
        }
        int temp = st.top();
        st.pop();
        sortedInsert(st, x);
        st.push(temp);
    }
    

    What it does:
    It inserts x into the stack st in sorted order.

    How?

    • If stack is empty OR the top element is greater than x, push x directly (because it’s in the right position).
    • Otherwise, remove the top element temporarily, call sortedInsert recursively to insert x in the smaller stack.
    • After that, put back the temporarily removed element on top.

    Function: sortStack of Sorting a Stack Using Recursion

    void sortStack(stack<int> &st) {
        if (st.empty()) return;
    
        int x = st.top();
        st.pop();
    
        sortStack(st);
    
        sortedInsert(st, x);
    }
    

    What it does:
    Sorts the stack by removing elements one by one and inserting them back in the sorted order.

    How?

    • Remove the top element x.
    • Recursively sort the smaller stack.
    • Insert the removed element x back into the sorted stack using sortedInsert.

    main Function — Driver Code of Sorting a Stack Using Recursion

    int main() {
        stack<int> st;
    
        st.push(30);
        st.push(10);
        st.push(50);
        st.push(20);
        st.push(40);
    
        // Print original stack
        cout << "Original Stack (top to bottom): ";
        stack<int> temp = st;
        while (!temp.empty()) {
            cout << temp.top() << " ";
            temp.pop();
        }
        cout << endl;
    
        // Sort the stack
        sortStack(st);
    
        // Print sorted stack
        cout << "Sorted Stack (top to bottom): ";
        while (!st.empty()) {
            cout << st.top() << " ";
            st.pop();
        }
        cout << endl;
    
        return 0;
    }
    

    Dry Run (Step by Step) | Sorting a Stack Using Recursion

    Let’s dry run with the stack having elements (top to bottom):

    40 (top), 20, 50, 10, 30 (bottom)

    Step 1: sortStack is called with full stack

    • Pop 40
    • Recursively call sortStack with stack: 20, 50, 10, 30

    Step 2: sortStack called again

    • Pop 20
    • Recursively call sortStack with stack: 50, 10, 30

    Step 3: Next recursive call

    • Pop 50
    • Recursively call sortStack with stack: 10, 30

    Step 4:

    • Pop 10
    • Recursively call sortStack with stack: 30

    Step 5:

    • Pop 30
    • Recursively call sortStack with empty stack

    Step 6:

    • Stack is empty, return from recursion

    Step 7: Now insert back the elements in sorted order using sortedInsert

    • Insert 30 into empty stack → stack: 30

    Step 8: Insert 10 into stack 30

    • Compare top 30 > 10 → push 10 on top → stack: 10, 30

    Step 9: Insert 50 into stack 10, 30

    • 30 < 50, pop 30
    • 10 < 50, pop 10
    • Stack empty → push 50
    • Push back popped 10 → stack: 10, 50
    • Push back popped 30 → stack: 30, 10, 50
      Oops! This looks reversed — wait, order is important. Let’s check carefully:

    Actually, the function pushes back in this order:

    • After popping 30, then 10, stack empty
    • push 50 → stack: 50
    • push 10 → stack: 10, 50 (top is 10, then 50)
    • push 30 → stack: 30, 10, 50 (top is 30)

    But this is wrong, we want the smallest on the bottom.

    Actually, when printing, top is on the left.

    Wait, the way we visualize is top at left (in code top is accessed by st.top()), so the stack is:

    • Top: 30
    • then: 10
    • then: 50

    So 30 is on top, 10 below it, 50 at bottom → not sorted.

    So something is wrong in our understanding.

    But the code is correct. The explanation is: the stack stores elements with the top being the last pushed.

    Let’s clarify: In the code, the stack top is the last inserted, and when we print from top to bottom, we pop top elements.

    The sorted stack means top element is largest.

    So the sorted stack (top to bottom): 50 40 30 20 10

    Let’s do Step 9 carefully:

    Stack before insertion: 10 (top), 30

    Insert 50:

    • top is 10, which is not greater than 50 → pop 10
    • next top is 30, which is not greater than 50 → pop 30
    • stack empty → push 50
    • push back 30
    • push back 10

    Stack now:
    Top → 10, then 30, then 50 (bottom)

    Printing top to bottom: 10 30 50

    So stack is not sorted this way.

    So the stack sorted order after inserting elements means smallest at bottom, largest at top:

    This means top is largest.

    After all recursion and insertions, final stack will be:

    50 (top), 40, 30, 20, 10 (bottom)

    which is sorted in ascending order if you read bottom to top.

    Summary of how recursion works here

    • We remove elements one by one until stack is empty.
    • Then we insert elements back in correct sorted order.
    • sortedInsert helps put element x in correct position by temporarily popping elements greater than x.
    • After insertion, the stack remains sorted from bottom (smallest) to top (largest).

    Practice Tip:

    To better understand how the stack changes during the sorting process, try adding cout (print) statements inside the functions. For example, print the current elements in the stack each time you push or pop something. This way, you can see step-by-step how elements move in and out of the stack. It helps you visualize the flow of recursion and makes it easier to follow the logic.

    Watching the stack change in real time is a great way to learn and debug your code!

    Practice Problems

    1. Reverse a Stack Using Recursion
      Write a recursive function to reverse the elements of a stack without using any extra stack or array.
    2. Insert an Element at the Bottom of a Stack
      Write a recursive function that inserts a given element at the bottom of a stack.
    3. Sort a Stack Without Recursion
      Try to sort a stack using only stack operations (push, pop, top) but without recursion. You may use an additional stack or queue.
    4. Find the Maximum Element in a Stack Using Recursion
      Write a recursive function that returns the maximum element present in a stack.
    5. Delete the Middle Element of a Stack Using Recursion
      Given a stack, write a recursive function to delete the middle element of the stack without using any extra space.

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on EmbeddedPrep

  • How to Reverse a Stack Using Recursion | Master Beginner Friendly guide 2026

    Reverse a Stack Using Recursion : Stacks are an important data structure in programming. They work like a stack of plates — you can only add or remove plates from the top. This behavior is called LIFO (Last In, First Out).

    Sometimes, you might want to reverse a stack, so the element that was at the top moves to the bottom and vice versa. But how do you reverse a stack without using extra memory or another stack? The answer is recursion!

    In this article, we’ll learn how to reverse a stack using recursion by inserting elements at the bottom.

    What is a Stack?

    A stack allows only two main operations:

    • push(x): Add element x to the top.
    • pop(): Remove the top element.

    You cannot directly access or modify elements below the top.

    Problem Statement

    Given a stack, reverse the order of elements using recursion. For example:

    Original stack (top to bottom):
    5, 4, 3, 2, 1

    After reversing, the stack should be:
    1, 2, 3, 4, 5

    Key Idea: Insert at Bottom

    Since we can only add or remove elements from the top, how do we insert an element at the bottom?

    We use recursion:

    1. If the stack is empty, push the element — that’s the bottom.
    2. Otherwise, pop the top element, call the function recursively to insert the element at the bottom, then push the popped element back on top.

    This way, the new element ends up at the bottom, and the original elements remain on top.

    Step-by-Step Solution

    1. Write a function to insert an element at the bottom:

    void insertAtBottom(stack<int> &st, int x) {
        if (st.empty()) {
            st.push(x);  // If empty, push element (bottom)
            return;
        }
        
        int topElement = st.top();
        st.pop();  // Remove top element
        
        insertAtBottom(st, x);  // Recursive call
        
        st.push(topElement);  // Push the popped element back on top
    }
    

    2. Write a function to reverse the stack:

    void reverseStack(stack<int> &st) {
        if (st.empty()) return;  // Base case
        
        int topElement = st.top();
        st.pop();  // Remove top
        
        reverseStack(st);  // Reverse remaining stack
        
        insertAtBottom(st, topElement);  // Insert removed element at bottom
    }
    

    How Does This Work?

    • reverseStack() keeps popping the top element until the stack is empty.
    • Then it starts inserting each popped element at the bottom of the stack.
    • This reverses the order because the first popped element (which was originally on top) goes to the bottom.

    Full Working Code

    #include <iostream>
    #include <stack>
    using namespace std;
    
    // Insert element x at the bottom of stack st
    void insertAtBottom(stack<int> &st, int x) {
        if (st.empty()) {
            st.push(x);
            return;
        }
        int topElement = st.top();
        st.pop();
        insertAtBottom(st, x);
        st.push(topElement);
    }
    
    // Reverse the stack using recursion
    void reverseStack(stack<int> &st) {
        if (st.empty()) return;
        int topElement = st.top();
        st.pop();
        reverseStack(st);
        insertAtBottom(st, topElement);
    }
    
    // Print stack elements from top to bottom
    void printStack(stack<int> st) {
        while (!st.empty()) {
            cout << st.top() << " ";
            st.pop();
        }
        cout << endl;
    }
    
    int main() {
        stack<int> st;
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);
        st.push(5);
    
        cout << "Original Stack: ";
        printStack(st);
    
        reverseStack(st);
    
        cout << "Reversed Stack: ";
        printStack(st);
    
        return 0;
    }
    

    Output:

    Original Stack: 5 4 3 2 1
    Reversed Stack: 1 2 3 4 5
    

    Conclusion

    By cleverly using recursion, we can reverse a stack without using any extra data structure like another stack or array.

    The trick lies in the helper function insertAtBottom() which helps us insert an element at the bottom of the stack, allowing us to place elements in reverse order during recursion unwinding.

    If you want to practice further, try these exercises:

    • Write a function to reverse a stack without recursion (using an extra stack or queue).
    • Modify the code to reverse a stack of strings instead of integers.
  • Insert an Element at the Bottom of a Stack | Master Beginner-Friendly Guide 2026

    Insert an Element at the Bottom of a Stack : Want to master stacks? This beginner-friendly tutorial walks you through how to insert an element at the bottom of a stack using recursion — without any extra data structures!

    You’ll learn:

    ✅ What the problem means
    ✅ How recursion helps you simulate going deep into the stack
    ✅ Step-by-step explanation with dry run
    ✅ Full working code in C++ and Python
    ✅ Practice exercises to test your understanding

    Perfect for beginners preparing for coding interviews or learning recursion concepts with stacks. Start building your foundation in data structures now!

    Problem Statement Insert an Element at the Bottom of a Stack:

    “Given a stack and an element, insert that element at the bottom of the stack.”

    Example:
    Input: Stack = 5 4 3 2 1, Element to Insert = 10
    Output: Stack = 10 5 4 3 2 1

    Concepts You’ll Learn:

    • Stack basics
    • Recursion
    • How function calls can act like a temporary stack

    Approach for :

    Step-by-step solution for Insert an Element at the Bottom of a Stack:

    1. If the stack is empty, push the given element. ✅
    2. Otherwise:
      • Pop the top element temporarily.
      • Recur to insert the element at the bottom.
      • Push the popped elements back after inserting. 🔄

    This way, you dig down to the bottom using recursion, then insert, and rebuild the stack on your way back up.

    C++ Code for Insert an Element at the Bottom of a Stack

    #include <iostream>
    #include <stack>
    using namespace std;
    
    // Function to insert element at the bottom of a stack
    void insertAtBottom(stack<int>& st, int element) {
        // Base case: stack is empty
        if (st.empty()) {
            st.push(element);
            return;
        }
    
        // Step 1: Pop top element
        int top = st.top();
        st.pop();
    
        // Step 2: Recursive call
        insertAtBottom(st, element);
    
        // Step 3: Push top back after inserting
        st.push(top);
    }
    
    int main() {
        stack<int> st;
    
        // Push some elements
        st.push(1);
        st.push(2);
        st.push(3);
        st.push(4);
        st.push(5);
    
        int newElement = 10;
        insertAtBottom(st, newElement);
    
        // Display stack
        cout << "Stack after inserting at bottom:\n";
        while (!st.empty()) {
            cout << st.top() << " ";
            st.pop();
        }
    
        return 0;
    }
    

    Python Code for Insert an Element at the Bottom of a Stack

    def insert_at_bottom(stack, element):
        # Base case: if stack is empty, insert element
        if not stack:
            stack.append(element)
            return
    
        # Step 1: Remove top
        top = stack.pop()
    
        # Step 2: Recursive call
        insert_at_bottom(stack, element)
    
        # Step 3: Put back the top
        stack.append(top)
    
    # Test
    stack = [1, 2, 3, 4, 5]
    element = 10
    insert_at_bottom(stack, element)
    
    print("Stack after inserting at bottom:")
    print(stack[::-1])  # Print from top to bottom
    

    Dry Run Example (Simple):

    Let’s dry run the C++/Python code for inserting an element at the bottom of a stack, step by step.

    Input:

    Initial Stack (top -> bottom): [5, 4, 3, 2, 1]
    Element to insert at bottom: 10
    

    Function Call:

    insertAtBottom(st, 10);
    

    Dry Run – Step by Step (Recursion Unwinding)

    Stack Before:

    [top] → 5 → 4 → 3 → 2 → 1 → [bottom]

    Step 1:

    st.top() is 5
    Pop 5 → Recur on smaller stack

    Call: insertAtBottom([4, 3, 2, 1], 10)

    Step 2:

    Pop 4 → Recur again
    Call: insertAtBottom([3, 2, 1], 10)

    Step 3:

    Pop 3 → Recur again
    Call: insertAtBottom([2, 1], 10)

    Step 4:

    Pop 2 → Recur again
    Call: insertAtBottom([1], 10)

    Step 5:

    Pop 1 → Recur again
    Call: insertAtBottom([], 10)

    Step 6 (Base Case):

    Stack is empty
    → Push 10

    ✅ Stack: [10]

    Backtracking Begins (Pushing elements back):

    Now we return from previous function calls one-by-one:

    Return to Step 5:
    Push 1 → Stack: [1, 10]

    Return to Step 4:
    Push 2 → Stack: [2, 1, 10]

    Return to Step 3:
    Push 3 → Stack: [3, 2, 1, 10]

    Return to Step 2:
    Push 4 → Stack: [4, 3, 2, 1, 10]

    Return to Step 1:
    Push 5 → Stack: [5, 4, 3, 2, 1, 10]

    Final Stack (Top to Bottom):

    [top] → 5 → 4 → 3 → 2 → 1 → 10 → [bottom]

    🎉 Success! We inserted 10 at the bottom of the stack without using extra space

    Why Use Recursion?

    Stacks follow Last-In-First-Out (LIFO) — meaning you can only directly access or modify the top of the stack. There’s no built-in way to reach the bottom element directly.

    But recursion gives us a trick:

    • It lets us temporarily pop all elements one by one.
    • Then, after reaching the empty stack (i.e., bottom), we insert the new element.
    • Finally, recursion rebuilds the original stack by pushing the saved elements back.

    So, recursion simulates going deep into the stack, allowing us to insert at the bottom without breaking the stack’s rules!

    Practice Exercises (to solidify your learning):

    🔁 1. Reverse a stack using the insert_at_bottom() method.
    💡 Tip: Use recursion to pop all elements, then reinsert using your insert-at-bottom function.

    🔁 2. Insert an element at the bottom without using recursion, by using:

    • A second temporary stack, or
    • A queue (FIFO nature)

    🔁 3. Implement stack reversal using both iterative and recursive approaches.
    🔁 4. Write a function to find the size of a stack recursively.
    🔁 5. Use insert_at_bottom() to sort a stack recursively in increasing order.

    You can also Visit other tutorials of Embedded Prep 

  • Valid Parentheses Problem stack |Beginner Friendly Explanation | Leetcode Solution (2026)

    The Valid Parentheses Problem is a classic example of how stack data structures can be used to solve real-world problems involving nested or paired data. The challenge is to determine whether a given string containing only brackets—(, ), {, }, [, ]—is properly balanced and well-formed.

    In a valid expression:

    • Every opening bracket must have a corresponding closing bracket.
    • Brackets must close in the correct order, maintaining the expected nesting.

    To solve this, a stack is used to keep track of opening brackets. When a closing bracket appears, the algorithm checks whether it correctly matches the most recent unclosed opening bracket by looking at the top of the stack. If it doesn’t match or the stack is empty when it shouldn’t be, the expression is invalid. If the stack is empty after processing all characters, the expression is valid.

    Valid Parentheses Problem stack description:

    You are given a string that contains only the characters:

    '(', ')', '{', '}', '[', ']'

    Your task is to check whether the given string is a valid sequence of parentheses.

    A valid string must follow these rules:

    1. Every opening bracket must have a corresponding closing bracket.
    2. The brackets must close in the correct order.
    3. Each type of bracket must match correctly (() not [(]).

    Why Use a Stack?

    A stack is a data structure that works on Last In, First Out (LIFO). That means:

    • The last element you put in is the first one that comes out.
    • This is perfect for tracking open brackets and checking if they are closed properly.

    Step-by-Step Logic for Valid Parentheses Problem stack:

    1. Create an empty stack.
    2. Loop through each character in the string.
    3. If it is an opening bracket ((, {, [), push it onto the stack.
    4. If it is a closing bracket:
      • Check if the stack is empty → if yes, return false.
      • Pop the top element from the stack.
      • Check if the popped element matches the current closing bracket → if not, return false.
    5. After the loop, if the stack is empty, return true (valid). If not, return false (invalid).

    Example of Valid Parentheses Problem stack:

    Input: "{[()]}"
    Output: true
    Explanation: All brackets open and close in the correct order.

    Input: "{[(])}"
    Output: false
    Explanation: [ is not properly closed before ) comes.

    Key Concepts Covered:

    • Stack data structure
    • Matching pairs of brackets
    • Input validation
    • Control flow with conditionals

    Problem Statement Valid Parentheses Problem stack:

    Given a string containing just the characters '(', ')', '{', '}', '[', and ']', determine if the input string is valid.

    👉 A string is valid if:

    1. Open brackets are closed by the same type of brackets.
    2. Open brackets are closed in the correct order.
    3. Every closing bracket has a corresponding open bracket.

    Example:

    Input: "({[]})"
    Output: true
    
    Input: "({[})"
    Output: false
    
    Input: "((("
    Output: false
    

    Approach: Use a Stack

    • Push opening brackets into a stack.
    • For every closing bracket:
      • Check if the stack is empty → ❌ Invalid
      • Check if the top of the stack is the matching opening bracket
    • In the end, the stack should be empty → ✅ Valid

    C++ Code (Beginner-Friendly) | Valid Parentheses Problem stack

    #include <iostream>
    #include <stack>
    #include <string>
    using namespace std;
    
    bool isValid(string s) {
        stack<char> st;
    
        for (char ch : s) {
            // Push opening brackets
            if (ch == '(' || ch == '{' || ch == '[') {
                st.push(ch);
            } else {
                // If stack is empty when closing bracket comes → invalid
                if (st.empty()) return false;
    
                char top = st.top();
                st.pop();
    
                // Check matching pairs
                if ((ch == ')' && top != '(') ||
                    (ch == '}' && top != '{') ||
                    (ch == ']' && top != '[')) {
                    return false;
                }
            }
        }
    
        // Stack should be empty if all brackets matched
        return st.empty();
    }
    
    int main() {
        string str = "({[]})";
        if (isValid(str)) {
            cout << "Valid" << endl;
        } else {
            cout << "Invalid" << endl;
        }
        return 0;
    }
    

    Dry Run Example: "({[]})"

    StepCharStackAction
    1((Push
    2{( {Push
    3[( { [Push
    4]( {Pop + Match [
    5}(Pop + Match {
    6)(empty)Pop + Match (

    → Stack is empty ✅ → Valid

    Python Code (Beginner-Friendly) | Valid Parentheses Problem stack

    def isValid(s):
        stack = []
        mapping = {')': '(', '}': '{', ']': '['}
    
        for char in s:
            if char in mapping.values():
                stack.append(char)
            elif char in mapping:
                if not stack or stack[-1] != mapping[char]:
                    return False
                stack.pop()
            else:
                # In case of unexpected character
                return False
    
        return not stack
    
    # Example usage
    s = "{[()]}"
    print("Valid" if isValid(s) else "Invalid")
    

    Java Code (Beginner-Friendly) | Valid Parentheses Problem stack

    import java.util.Stack;
    
    public class ValidParentheses {
    
        public static boolean isValid(String s) {
            Stack<Character> stack = new Stack<>();
            
            for (char ch : s.toCharArray()) {
                if (ch == '(' || ch == '{' || ch == '[') {
                    stack.push(ch);
                } else {
                    if (stack.isEmpty()) return false;
    
                    char top = stack.pop();
                    if ((ch == ')' && top != '(') ||
                        (ch == '}' && top != '{') ||
                        (ch == ']' && top != '[')) {
                        return false;
                    }
                }
            }
            return stack.isEmpty();
        }
    
        public static void main(String[] args) {
            String s = "{[()]}";
            System.out.println(isValid(s) ? "Valid" : "Invalid");
        }
    }

    Beginner-Level Practice Problems

    1. Basic Valid Parentheses

    • 🔹 Problem: Check if the given string with only ()[]{} is valid.
    • 🔹 Example: "([])" → true, "(]" → false
    • 🔹 Goal: Implement the classic stack-based solution.

    2. Minimum Add to Make Parentheses Valid

    • 🔹 Problem: Given a string of parentheses '(' and ')', return the minimum number of additions needed to make it valid.
    • 🔹 Example: "(()" → 1, "()))(" → 3
    • 🔹 LeetCode #921

    3. Longest Valid Parentheses

    • 🔹 Problem: Given a string of '(' and ')', find the length of the longest valid (well-formed) parentheses substring.
    • 🔹 Example: "(()" → 2, ")()())" → 4
    • 🔹 LeetCode #32

    4. Remove Invalid Parentheses

    • 🔹 Problem: Remove the minimum number of invalid parentheses to make the input string valid. Return all possible results.
    • 🔹 Example: "(a)())()" → ["(a())()", "(a)()()"]
    • 🔹 LeetCode #301

    5. Score of Parentheses

    • 🔹 Problem: Compute the score of a balanced parentheses string.
    • 🔹 Rules: "()" has score 1, "AB" has score A + B, "(A)" has score 2 * A
    • 🔹 Example: "(()(()))" → 6
    • 🔹 LeetCode #856

    Bonus Challenge

    6. Validate Stack Sequences

    • 🔹 Problem: Given two sequences pushed and popped, return true if they could represent the push and pop sequence of a single stack.
    • 🔹 LeetCode #946

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on EmbeddedPrep

  • Master Structures in C – A Beginner’s Guide with Interview Questions (2026)

    Structures in C : Unlock the power of structures in C programming with this beginner-friendly guide tailored for 2025 tech interviews! Whether you’re new to C or brushing up for embedded and systems programming interviews, this post offers a clear and practical explanation of structures — from basic syntax to advanced concepts like structure padding, bit-fields, and pointers to structures.

    Dive deep into real-world examples, memory layout illustrations, and commonly asked interview questions that help you build a strong foundation and gain confidence in handling Structures data in C.

    What You’ll Learn:

    • What is a structure and why it’s used in C
    • How to define, initialize, and access structure members
    • Structure padding and memory alignment
    • Bit-fields and their practical usage
    • Structure pointers and dynamic memory allocation
    • Array of structures and nested structures
    • Top interview questions and expert tips

    Perfect for students, job-seekers, and professionals aiming to master data organization in C, this guide equips you with everything you need to ace interviews and write better, cleaner C code.

    What is a Structure in C?

    A structure is a user-defined data type that allows combining variables of different types under one name. Each variable inside a structure is called a member of the structure.

    Syntax:

    struct StructureName {
        dataType member1;
        dataType member2;
        ...
    };
    

    Example:

    #include <stdio.h>
    
    struct Student {
        int id;
        char name[50];
        float marks;
    };
    
    int main() {
        struct Student s1 = {1, "John", 87.5};
        
        printf("ID: %d\n", s1.id);
        printf("Name: %s\n", s1.name);
        printf("Marks: %.2f\n", s1.marks);
        
        return 0;
    }
    

    Key Concepts:

    Declaration

    You can declare a structure type and create variables:

    struct Employee {
        int emp_id;
        float salary;
    } e1, e2;
    

    Accessing Structure Members

    Use the dot (.) operator:

    e1.emp_id = 101;
    e1.salary = 50000.00;
    

    Structure Pointer and Arrow (->) Operator

    struct Employee *ptr = &e1;
    ptr->salary = 55000.00;
    

    Nested Structures

    struct Date {
        int day, month, year;
    };
    
    struct Employee {
        int id;
        struct Date joiningDate;
    };
    

    Array of Structures

    struct Student students[5];
    

    Why Use Structures?

    • To group logically related data
    • To create complex data models
    • To make code more readable and maintainable
    • Crucial for hardware register mapping in embedded C

    Memory Layout of Structures

    Structure members are stored sequentially in memory, and padding may be added for alignment. You can control this using #pragma pack (compiler-specific).

    Important Notes:

    • You can’t initialize members inside the struct definition.
    • Structures can’t contain functions, but they can contain function pointers.
    • sizeof(struct) returns the size including padding bytes.
    • You can pass structure to functions by value or pointer.

    What is the size of a structure in C if nothing is defined inside it?

    In Standard C:

    In standard C (C89/C90 and later), a structure must contain at least one member. An empty structure is not allowed and will typically give a compiler error.

    Example:

    struct Empty {
        // nothing inside
    };
    
    int main() {
        struct Empty e;
        printf("%lu", sizeof(e));
        return 0;
    }
    

    Output (for most compilers like GCC):

    error: structure has no members
    

    In GNU C (GCC) – Extension:

    GCC does allow empty structures as a compiler extension (used sometimes in low-level code or placeholders). In such cases:

    • The size of an empty struct is typically 1 byte, to ensure each variable has a unique address.

    ✔️ Example (GCC):

    struct Empty { };
    
    int main() {
        struct Empty e;
        printf("%zu\n", sizeof(e)); // Output: 1
        return 0;
    }
    

    Why 1 byte?

    This is done to ensure that even an empty structure occupies a unique memory location (not zero bytes), which is important for things like:

    • Arrays of structures
    • Memory alignment
    • Pointers

    Summary Table:

    CompilerEmpty struct allowed?sizeof(struct)
    GCC✅ (extension)1 byte
    Clang✅ (extension)1 byte
    MSVC❌ (error)Compilation error
    Standard C❌ (not allowed)Compilation error

    Interview Tip:

    Q: What is the size of an empty structure in C?

    A: In standard C, empty structures are not allowed and give a compile-time error. But in GCC (as an extension), it is allowed and the size is 1 byte.

    Structure Padding in C

    What is Padding?

    Padding is the process of adding unused memory (bytes) between structure members to align data in memory according to the CPU architecture’s alignment requirements.

    Why is Padding Needed?

    Most processors access data faster when it’s aligned to its natural boundary:

    • int (4 bytes) should be stored at addresses divisible by 4
    • short (2 bytes) at addresses divisible by 2, etc.

    To maintain this alignment, compilers insert padding bytes between structure members.

    Example of Padding:

    #include <stdio.h>
    
    struct A {
        char a;   // 1 byte
        int b;    // 4 bytes
    };
    
    int main() {
        printf("Size of struct A: %lu\n", sizeof(struct A));
        return 0;
    }
    

    Output:

    Size of struct A: 8
    

    Explanation:

    MemberSizeOffsetNotes
    a1 B0
    padding3 B1-3for alignment
    b4 B4-7aligned on 4-byte boundary

    How to Minimize Padding?

    1. Reorder members from largest to smallest:
    struct B {
        int b;   // 4 bytes
        char a;  // 1 byte
    };
    

    ➡️ sizeof(struct B) will be 8, but more efficient than previous layout.

    1. Use #pragma pack(1) to disable padding (GCC-specific):
    #pragma pack(1)
    struct Packed {
        char a;
        int b;
    };
    #pragma pack()
    

    But be careful—disabling padding can cause slower access or alignment faults on some hardware (especially embedded/ARM CPUs).

    What is typedef in C?

    typedef is a keyword in C (and C++) used to create a new alias (name) for an existing data type. It helps in making code more readable, portable, and easier to maintain.

    Syntax:

    typedef existing_type new_type_name;
    

    Basic Example:

    typedef unsigned int uint;
    
    uint a = 10;   // Same as: unsigned int a = 10;
    

    Here, uint is a new name (alias) for unsigned int.

    Why Use typedef?

    PurposeBenefit
    Simplify complex typesMakes code readable and reduces clutter
    Increase portabilityAbstracts platform-dependent types (e.g., typedef int int32_t)
    Easier maintenanceUpdate one typedef instead of multiple changes in code
    Used with structsGives simple names to struct types without needing struct every time

    Common Use with struct:

    struct Student {
        int id;
        char name[50];
    };
    
    typedef struct Student Student;
    

    Now you can just write:

    Student s1;
    

    Instead of:

    struct Student s1;
    

    Or even more compact:

    typedef struct {
        int id;
        char name[50];
    } Student;
    

    With Pointers:

    typedef int* IntPtr;
    
    IntPtr p1, p2;  // Both p1 and p2 are int pointers
    

    Common Mistake:

    typedef int* IntPtr;
    
    IntPtr a, b;
    

    Many think both a and b are int*, but they are. This is correct.

    But if written as:

    int* a, b;  // a is int*, b is int (confusing)
    

    This is why typedef helps avoid confusion.

    Summary:

    FeatureDescription
    Keywordtypedef
    PurposeRename/alias a data type
    UsageUseful with structs, complex pointers
    BenefitImproves code clarity and maintainability

    Bit Fields in Structures

    What Are Bit Fields?

    Bit fields allow you to allocate specific number of bits to structure members instead of full bytes.

    Syntax:

    struct Flags {
        unsigned int a : 1;
        unsigned int b : 2;
        unsigned int c : 5;
    };
    
    • a uses 1 bit
    • b uses 2 bits
    • c uses 5 bits

    This is useful in:

    • Embedded systems (e.g., setting hardware registers)
    • Flag control
    • Memory optimization

    Example:

    #include <stdio.h>
    
    struct Status {
        unsigned int error : 1;
        unsigned int ready : 1;
        unsigned int mode : 2;
    };
    
    int main() {
        struct Status s = {1, 0, 2};
        printf("Size of Status: %lu\n", sizeof(s));
        return 0;
    }
    

    Output:

    Size of Status: 4
    

    Even though total bits = 1 + 1 + 2 = 4 bits, size is still rounded to nearest word size (here: 4 bytes on 32-bit machine).

    Rules and Notes:

    • Bit fields must be of integer type
    • You can’t take address of bit field
    • Compiler decides bit packing and alignment
    • Use unsigned int to avoid sign extension issues

    Structure Pointers

    What is a Structure Pointer?

    A pointer to a structure stores the address of a structure variable. This is useful for:

    • Passing structures efficiently to functions
    • Dynamic memory allocation
    • Embedded register access

    Example:

    #include <stdio.h>
    
    struct Student {
        int id;
        char name[20];
    };
    
    int main() {
        struct Student s = {101, "John"};
        struct Student *ptr = &s;
    
        printf("ID: %d\n", ptr->id);         // OR (*ptr).id
        printf("Name: %s\n", ptr->name);
        
        return 0;
    }
    

    Access Operators:

    SyntaxMeaning
    ptr->memberShortcut to access
    (*ptr).memberLonger version

    Both work the same.

    Use in Dynamic Memory:

    struct Student *ptr = malloc(sizeof(struct Student));
    ptr->id = 10;
    strcpy(ptr->name, "Alice");
    

    Real-World Use Case: Register Mapping in Embedded C

    #define UART0_BASE 0x4000C000
    
    struct UART {
        volatile uint32_t DR;
        volatile uint32_t SR;
    };
    
    #define UART0 ((struct UART *)UART0_BASE)
    

    Now you can write:

    UART0->DR = 0x55;

    Interview Questions on Structures in C

    Beginner Level

    1. What is a structure in C?
    2. What are the advantages of using structures?
    3. How do you declare and access a structure member?
    4. What is the difference between structure and array?
    5. Can we initialize members inside the structure?

    Intermediate Level

    1. How can you pass a structure to a function?
    2. What is the difference between . and -> operators?
    3. Explain nested structures with an example.
    4. What is the size of an empty structure?
    5. Can a structure contain another structure?

    Advanced/Embedded Level

    1. How is memory aligned in structures?
    2. How to minimize structure padding in embedded C?
    3. Can a structure contain a pointer to itself?
    4. Explain structure packing using #pragma pack.
    5. How are structures used in embedded register mapping?
    6. What is structure padding and how can you avoid it?
    7. Why does the compiler insert padding bytes in a structure?
    8. What are bit fields and where are they used?
    9. Can we take the address of a bit field? Why or why not?
    10. Explain structure pointers and -> operator.
    11. How are structure pointers useful in embedded system programming?
    12. What happens when you use #pragma pack(1)?
    13. Can we dynamically allocate memory for a structure?
    14. How can structure padding affect data sent over network or stored in file?
    15. Can a struct contain a pointer to itself? What is a self-referential structure?

    Sample Embedded Interview Scenario

    struct UART_Registers {
        volatile uint32_t DR;
        volatile uint32_t SR;
        volatile uint32_t CR;
    };
    
    #define UART1 ((struct UART_Registers *) 0x40011000)
    

    Question: What does this code do?

    ✅ Answer: It defines a structure for UART registers and maps it to the memory-mapped I/O address 0x40011000.

    Summary

    FeatureStructure
    Custom Data TypeYes
    Mixed Data TypesYes
    Can be nestedYes
    Dot and Arrow AccessYes
    Memory EfficientWith alignment
    Real-world use casesSystem-level data modeling, embedded register mapping

    📘 Practice Task

    Create a structure to store employee details (name, id, department, salary), and write a C program to:

    • Read data of 5 employees
    • Print details of the highest-paid employee

    You can also Visit other tutorials of Embedded Prep 

    Special thanks to @mr-raj for contributing to this article on EmbeddedPrep