Interview Questions on 1D & 2D Arrays (C Language)

📘 1D Array – Short Answer Interview Questions

  1. What is the default value of elements in a statically declared array in C?
    Undefined for local arrays. For global/static arrays, it's 0.
  2. How do you calculate the length of a 1D array in C?
    Use sizeof(arr)/sizeof(arr[0]) when array is in scope.
  3. What happens if you access an array index out of bounds in C?
    Results in undefined behavior and may cause a crash or data corruption.
  4. Can you change the size of a statically declared array after declaration?
    No. Static arrays have fixed size determined at compile time.
  5. Difference between int arr[] and int *arr in function parameters?
    Both decay to pointers, but pointer arithmetic and sizeof behave differently.
  6. What does sizeof(arr) return in a function if arr is a parameter?
    It returns the size of a pointer, not the whole array.
  7. How do you reverse a 1D array in place?
    Swap elements from ends towards center using a loop.
  8. What is a sentinel value in the context of arrays?
    A special value used to mark the end of useful data (e.g., -1 or 999).
  9. Can an array index be a negative value in C?
    Technically allowed but leads to undefined behavior.
  10. Why is array indexing in C zero-based?
    It simplifies pointer arithmetic and memory offset calculation.
  11. How is memory allocated for a 1D array declared globally vs locally?
    Global: stored in data segment; Local: stored on the stack.
  12. Explain the difference between arr[i] and *(arr + i).
    They are functionally equivalent due to pointer arithmetic.
  13. How do you initialize all elements of a 1D array to zero?
    Use int arr[10] = {0};
  14. Can you assign one array to another directly using = operator?
    No. You must copy elements individually or use memcpy.
  15. What is the use of const in const int arr[]?
    Prevents modification of array elements.
  16. How do you sort an array without using library functions?
    Use basic sorting algorithms like bubble sort or selection sort.
  17. What is the role of static keyword when used with arrays inside a function?
    Preserves array content between function calls.
  18. What is the difference between malloc and calloc for arrays?
    calloc initializes memory to zero; malloc does not.
  19. What does the realloc function do in array context?
    Resizes a dynamically allocated array while preserving content.
  20. How do you pass a portion (subarray) of a 1D array to a function?
    Pass the address of the starting index: func(&arr[start]).
  21. What is pointer arithmetic and how is it related to arrays?
    Pointer arithmetic lets you navigate array elements using pointer offsets.
  22. How do you implement binary search on a sorted array?
    Use divide-and-conquer: compare mid, then search left/right half recursively or iteratively.
  23. How do you find the duplicate element in an array where elements range from 1 to n-1?
    Use XOR or hashing or Floyd's cycle detection method.
  24. What is a dynamic array and how is it implemented in C?
    It's an array created using malloc/realloc to grow/shrink at runtime.
  25. How do you rotate an array by k elements?
    Use array reversal technique or temporary storage.
  26. Can arrays be returned from functions in C?
    Not directly. Return a pointer to dynamically allocated memory instead.
  27. What's the difference between shallow and deep copying arrays?
    Shallow copy copies pointer only; deep copy duplicates contents.
  28. How do you merge two sorted arrays into one?
    Use two-pointer technique to merge efficiently in O(n).
  29. What is the difference between stack and heap allocation of arrays?
    Stack: fixed size, fast. Heap: dynamic size, must be manually managed.
  30. What is the time complexity of inserting an element in the middle of an array?
    O(n) due to shifting of subsequent elements.

📗 2D Array – Short Answer Interview Questions

  1. How do you declare a 2D array in C?
    Example: int arr[3][4];
  2. What is the memory layout of a 2D array in C?
    Stored in row-major order (row by row in memory).
  3. Difference between int arr[3][4] and int **arr?
    First is a contiguous block; second is a pointer to pointer, may not be contiguous.
  4. Can you dynamically allocate a 2D array? How?
    Yes. Use malloc to allocate array of pointers, then allocate rows individually.
  5. How do you access the diagonal elements of a square matrix?
    Loop with arr[i][i] for primary, arr[i][n-i-1] for secondary diagonal.
  6. What is row-major and column-major ordering?
    Row-major stores rows together; column-major stores columns together (used in Fortran).
  7. How do you transpose a matrix in C?
    Swap elements: arr[i][j] with arr[j][i] for square matrices.
  8. How do you print the boundary elements of a matrix?
    Print top row, right col, bottom row reversed, and left col upwards (without repetition).
  9. How to find the maximum element in each row of a matrix?
    Iterate through each row, keep track of max value using a loop.
  10. How do you pass a 2D array to a function?
    Specify column size: void func(int arr[][COLS])
  11. Can you return a 2D array from a function?
    Return a pointer to dynamically allocated 2D memory.
  12. How do you flatten a 2D array into a 1D array?
    Use arr[i * cols + j] to map 2D to 1D.
  13. What happens if you access [i][j] out of bounds?
    Leads to undefined behavior or segmentation fault.
  14. What is a jagged array and can it be created in C?
    Jagged array has rows of different lengths. Simulate using array of pointers.
  15. How do you initialize a 2D array with all zeros?
    Use int arr[3][3] = {{0}};
  16. How do you find a saddle point in a matrix?
    An element that is minimum in its row and maximum in its column.
  17. How do you rotate a matrix 90 degrees clockwise?
    Transpose the matrix, then reverse each row.
  18. How to compute prefix sum in a 2D array?
    Use nested loop and store sum of previous rows/cols for each cell.
  19. What are common pitfalls in using dynamic 2D arrays?
    Not allocating memory for each row, memory leaks, forgetting to free memory.
  20. What is the space complexity of a 2D array?
    O(rows × columns)