Array Basics Interview Questions and Answers in C: Worked Traces and Traps

Build interview-ready answers for C arrays through exact memory, reversal, and second-largest traces, with the edge cases and reasoning behind each result.

KnowledgeGate Team

Exam prep & CS education

Updated 19 Nov 20266 min read
Array Basics Interview Questions and Answers in C: Worked Traces and Traps

Many candidates can declare an array, then stumble when asked why indexing starts at zero, whether an array is a pointer, what sizeof sees after a function call, or how a loop invariant proves correctness. Strong answers connect each definition to a reason, then trace memory layout, in-place reversal, and the second-largest distinct value. Fundamentals-first screening and deeper implementation follow-ups are useful preparation lenses, not fixed employer patterns.

Array basics: definition and properties

An array in C is a fixed-size, contiguous sequence of one element type. int a[5] has five int elements, indices 0 through 4, and a fixed size for that object. Some versions and implementations permit variable length arrays, but the indexing model is unchanged.

a[i] is defined as *(a + i). Index 0 therefore means zero elements from the first element, while index i has byte offset i * sizeof(element type). Valid-index access is O(1) address arithmetic, not a search.

Unsorted search is O(n). Front insertion or deletion in a packed array is O(n) because elements shift, but appending into available capacity in an array-backed container can be O(1). See Arrays in C: Declaration, Initialization and Worked Examples for the declaration foundation.

C array memory layout: five values and addresses

Take int a[5] = {12, 7, 19, 3, 11};. Assume sizeof(int) = 4 bytes and decimal base address &a[0] = 1000. Element i starts at 1000 + i * 4:

Element

Value

Address calculation

Start address

a[0]

12

1000 + 0 * 4

1000

a[1]

7

1000 + 1 * 4

1004

a[2]

19

1000 + 2 * 4

1008

a[3]

3

1000 + 3 * 4

1012

a[4]

11

1000 + 4 * 4

1016

Thus a[3] = *(a + 3) = 3, *(a + 4) = 11, and a[1 + 2] = a[3] = 3. The offset of a[4] from a[0] is 4 * 4 = 16 bytes. In the declaring scope, sizeof a = 5 * 4 = 20 bytes and sizeof a / sizeof a[0] = 20 / 4 = 5 elements.

Addresses 1000 through 1019 are occupied; only 1000, 1004, 1008, 1012, and 1016 start elements. C fixes neither a four-byte int nor address 1000; both are assumptions.

Diagram of a five-element C array showing values 12, 7, 19, 3, and 11 in contiguous cells with byte offsets and assumed decimal start addresses 1000 through 1016.

Is an array a pointer? sizeof and decay

No. a is an int[5] array object. In most expressions its name converts to a pointer to the first element, so a[i] equals *(a + i). The array owns five elements; a pointer stores a reassignable address. An array name is not a modifiable lvalue, so a++ and a = other are invalid.

In its declaring scope, sizeof a = 20 under our assumptions. In void inspect(int a[]), the parameter adjusts to int *a, so sizeof a is the pointer size, not 20. Assuming sizeof(int *) = 8, it yields 8. Pointer size is implementation-dependent; pass the element count separately.

a + 1 points to 1004. &a, type int (*)[5], starts numerically at 1000, but &a + 1 advances 20 bytes to 1020. Arrays and Strings in C: Decay, sizeof and 2D Addresses covers deeper cases.

Array bounds and initialization: four traps

What happens at a[5]? Valid indices are 0 through 4. The access has undefined behaviour: it need not wrap, return zero, or fail at compilation. Use i < 5, not i <= 5.

What does partial initialization do? int b[5] = {9, 4}; gives {9, 4, 0, 0, 0}. An uninitialised automatic local int b[5]; has indeterminate elements. Static-storage objects are zero-initialised.

Where does the count formula work? sizeof a / sizeof a[0] works where a is the array, not after parameter adjustment.

Can arrays be assigned? Direct left = right is invalid. Use suitable memcpy or an element loop. A character array initialised from a string literal may contain its terminating null character.

Reverse an array in place: exact swaps, invariant, and complexity

Reverse {4, 9, 1, 7, 3, 8} with left = 0, right = 5. Swap 0 and 5: {8, 9, 1, 7, 3, 4}; advance to left = 1, right = 4. Swap 1 and 4: {8, 3, 1, 7, 9, 4}; advance to left = 2, right = 3. Swap 2 and 3: {8, 3, 7, 1, 9, 4}. Now left = 3, right = 2, so left >= right stops the loop.

c
while (left < right) {
    temp = a[left];
    a[left] = a[right];
    a[right] = temp;
    left++;
    right--;
}

Each swap has one temporary and three assignments. After each iteration, elements outside [left, right] are in final reversed positions. This invariant proves that completed pairs need no revisit and crossing means stop.

For n = 6, floor(6 / 2) = 3 swaps. General cost is O(n) time and O(1) auxiliary space, excluding the input array's storage.

Diagram tracing in-place reversal of {4, 9, 1, 7, 3, 8} through three swaps to {8, 3, 7, 1, 9, 4}, with left and right indices.

Find the second-largest distinct value in one pass

For {7, 2, 9, 9, 5, 8}, begin with largest and second both not set. Trace every value:

Value read

(largest, second) after processing

7

(7, not set)

2

(7, 2)

first 9

(9, 7)

duplicate 9

(9, 7) unchanged

5

(9, 7) unchanged

8

(9, 8)

The answer is 8. If x > largest, move the old largest to second, then set largest = x. Otherwise update second only when x < largest and second is unset or x > second. Treat unset largest as the first update. Strict x < largest rejects the duplicate 9. Use explicit optional states, not a magic sentinel.

The scan is O(n) time and O(1) auxiliary space. {5, 5, 5} must return "no second-largest distinct value", not 5. Sorting works but exceeds the one-pass requirement.

How array-basics interviews move from recall to reasoning

Probe

Strong answer includes

Trap

Definition and indexing

Contiguous type; a[i] = *(a+i)

Calling zero arbitrary

Declaration and initialization

Fixed count; omitted values become zero

Every local starts at zero

Contiguous addresses

base + i * sizeof(element)

Universal sizes

Array conversion

Decay, not pointer identity

"Array equals pointer"

Bounds

0 through n-1

Using i <= n

Reversal

Invariant, O(n), O(1) space

Memorised loop only

Second-largest distinct

One pass, duplicates, failure

Accepting duplicate 9

A fundamentals-first lens covers declarations, output prediction, bounds, and loops. A depth-oriented lens adds invariants, edge cases, complexity, or distinct values. These are preparation lenses, not employer claims. Check current careers information or candidate communication.

The path has about 35 practice questions. That volume signals practice scope, not predicted frequency. Try C Programming Interview Questions for Freshers: 40 Answered for broader follow-ups and Placement Preparation for the wider route.

Array basics in 30 seconds, then the next step

An array stores same-type elements contiguously. a[i] means *(a + i), and valid bounds for length n are 0 through n - 1. The array converts to a pointer to its first element in most expressions, but it is not itself a pointer. A strong coding answer also states its invariant, edge cases, time complexity, and auxiliary space. For a structured sequence of concepts, MCQs, and coding questions, continue with the C Language course.