Interview Questions on 1D & 2D Arrays (C Language)
📘 1D Array – Short Answer Interview Questions
-
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.
-
How do you calculate the length of a 1D array in C?
Use
sizeof(arr)/sizeof(arr[0])when array is in scope. -
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.
-
Can you change the size of a statically declared array after declaration?
No. Static arrays have fixed size determined at compile time.
-
Difference between
int arr[]andint *arrin function parameters?Both decay to pointers, but pointer arithmetic and sizeof behave differently. -
What does
sizeof(arr)return in a function ifarris a parameter?It returns the size of a pointer, not the whole array. -
How do you reverse a 1D array in place?
Swap elements from ends towards center using a loop.
-
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).
-
Can an array index be a negative value in C?
Technically allowed but leads to undefined behavior.
-
Why is array indexing in C zero-based?
It simplifies pointer arithmetic and memory offset calculation.
-
How is memory allocated for a 1D array declared globally vs locally?
Global: stored in data segment; Local: stored on the stack.
-
Explain the difference between
arr[i]and*(arr + i).They are functionally equivalent due to pointer arithmetic. -
How do you initialize all elements of a 1D array to zero?
Use
int arr[10] = {0}; -
Can you assign one array to another directly using
=operator?No. You must copy elements individually or usememcpy. -
What is the use of
constinconst int arr[]?Prevents modification of array elements. -
How do you sort an array without using library functions?
Use basic sorting algorithms like bubble sort or selection sort.
-
What is the role of
statickeyword when used with arrays inside a function?Preserves array content between function calls. -
What is the difference between
mallocandcallocfor arrays?calloc initializes memory to zero; malloc does not. -
What does the
reallocfunction do in array context?Resizes a dynamically allocated array while preserving content. -
How do you pass a portion (subarray) of a 1D array to a function?
Pass the address of the starting index:
func(&arr[start]). -
What is pointer arithmetic and how is it related to arrays?
Pointer arithmetic lets you navigate array elements using pointer offsets.
-
How do you implement binary search on a sorted array?
Use divide-and-conquer: compare mid, then search left/right half recursively or iteratively.
-
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.
-
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.
-
How do you rotate an array by k elements?
Use array reversal technique or temporary storage.
-
Can arrays be returned from functions in C?
Not directly. Return a pointer to dynamically allocated memory instead.
-
What's the difference between shallow and deep copying arrays?
Shallow copy copies pointer only; deep copy duplicates contents.
-
How do you merge two sorted arrays into one?
Use two-pointer technique to merge efficiently in O(n).
-
What is the difference between stack and heap allocation of arrays?
Stack: fixed size, fast. Heap: dynamic size, must be manually managed.
-
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
-
How do you declare a 2D array in C?
Example:
int arr[3][4]; -
What is the memory layout of a 2D array in C?
Stored in row-major order (row by row in memory).
-
Difference between
int arr[3][4]andint **arr?First is a contiguous block; second is a pointer to pointer, may not be contiguous. -
Can you dynamically allocate a 2D array? How?
Yes. Use malloc to allocate array of pointers, then allocate rows individually.
-
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. -
What is row-major and column-major ordering?
Row-major stores rows together; column-major stores columns together (used in Fortran).
-
How do you transpose a matrix in C?
Swap elements:
arr[i][j]witharr[j][i]for square matrices. -
How do you print the boundary elements of a matrix?
Print top row, right col, bottom row reversed, and left col upwards (without repetition).
-
How to find the maximum element in each row of a matrix?
Iterate through each row, keep track of max value using a loop.
-
How do you pass a 2D array to a function?
Specify column size:
void func(int arr[][COLS]) -
Can you return a 2D array from a function?
Return a pointer to dynamically allocated 2D memory.
-
How do you flatten a 2D array into a 1D array?
Use
arr[i * cols + j]to map 2D to 1D. -
What happens if you access
[i][j]out of bounds?Leads to undefined behavior or segmentation fault. -
What is a jagged array and can it be created in C?
Jagged array has rows of different lengths. Simulate using array of pointers.
-
How do you initialize a 2D array with all zeros?
Use
int arr[3][3] = {{0}}; -
How do you find a saddle point in a matrix?
An element that is minimum in its row and maximum in its column.
-
How do you rotate a matrix 90 degrees clockwise?
Transpose the matrix, then reverse each row.
-
How to compute prefix sum in a 2D array?
Use nested loop and store sum of previous rows/cols for each cell.
-
What are common pitfalls in using dynamic 2D arrays?
Not allocating memory for each row, memory leaks, forgetting to free memory.
-
What is the space complexity of a 2D array?
O(rows × columns)
Related Array Resources