Placement Prep

Array Programs in C, C++ and Java: Most Asked Questions

Master the 6 core array patterns tested in AMCAT, TCS NQT, and company placement tests. Solutions in C, C++ and Java with time complexity.

By FACE Prep Team 7 min read
arrays c-programming cpp java placement-prep data-structures online-tests

Arrays appear in the coding section of AMCAT, TCS NQT, Cocubes, and most company-specific online tests. The six patterns below cover what consistently gets asked.

Why arrays dominate placement coding tests

A C array is a fixed block of contiguous memory. As the C/C++ language reference on cppreference.com notes, C arrays decay to pointers when passed to functions, which is why C and C++ functions always require the array size as a separate parameter. Java’s design is different: Java arrays are objects with a built-in .length property, as the Oracle Java Tutorials document. Java array functions therefore don’t need an explicit size argument.

These language differences don’t change the algorithm. Once you understand the logic for a pattern, translating it to C, C++, or Java is a syntax exercise more than anything else.

Placement tests favour arrays for a structural reason: a single 20-minute question can simultaneously test indexing, boundary handling, iteration, and time-space reasoning. The six patterns below handle what appears most often in these rounds.

The 6 core patterns tested in online rounds

Pattern 1: Find the minimum and maximum element

Initialise both maxVal and minVal to arr[0], then do a single pass from index 1 updating each as you go. A detailed walkthrough of this exact problem is in the find smallest and largest element in an array guide.

  • Time: O(n), Space: O(1)

C:

void findMinMax(int arr[], int n) {
    int maxVal = arr[0], minVal = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > maxVal) maxVal = arr[i];
        if (arr[i] < minVal) minVal = arr[i];
    }
    printf("Max: %d, Min: %d\n", maxVal, minVal);
}

C++:

void findMinMax(int arr[], int n) {
    int maxVal = arr[0], minVal = arr[0];
    for (int i = 1; i < n; i++) {
        if (arr[i] > maxVal) maxVal = arr[i];
        if (arr[i] < minVal) minVal = arr[i];
    }
    cout << "Max: " << maxVal << ", Min: " << minVal << endl;
}

Java:

static void findMinMax(int[] arr) {
    int maxVal = arr[0], minVal = arr[0];
    for (int i = 1; i < arr.length; i++) {
        if (arr[i] > maxVal) maxVal = arr[i];
        if (arr[i] < minVal) minVal = arr[i];
    }
    System.out.println("Max: " + maxVal + ", Min: " + minVal);
}

Pattern 2: Reverse an array in place

Two pointers: one starting at index 0, the other at n-1. Swap elements and move both pointers inward until they meet. No extra array needed.

  • Time: O(n), Space: O(1)

C:

void reverseArray(int arr[], int n) {
    int left = 0, right = n - 1;
    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}

C++:

void reverseArray(int arr[], int n) {
    int left = 0, right = n - 1;
    while (left < right) {
        swap(arr[left], arr[right]);
        left++;
        right--;
    }
}

Java:

static void reverseArray(int[] arr) {
    int left = 0, right = arr.length - 1;
    while (left < right) {
        int temp = arr[left];
        arr[left] = arr[right];
        arr[right] = temp;
        left++;
        right--;
    }
}

Pattern 3: Find the missing number in an array

Given n-1 integers drawn from the range 1 to n with exactly one missing, the sum-formula approach finds the answer in a single pass without sorting.

The approach uses three steps:

  • Compute expectedSum = n * (n + 1) / 2
  • Subtract the actual array sum from expectedSum
  • The difference is the missing number

For example, the array [1, 2, 4, 5] has n = 5 (four elements, range 1–5). The expected sum is 15, the actual sum is 12, so the missing number is 3.

C:

int findMissing(int arr[], int n) {
    int expected = n * (n + 1) / 2;
    int actual = 0;
    for (int i = 0; i < n - 1; i++) actual += arr[i];
    return expected - actual;
}

C++:

int findMissing(int arr[], int n) {
    int expected = n * (n + 1) / 2;
    int actual = 0;
    for (int i = 0; i < n - 1; i++) actual += arr[i];
    return expected - actual;
}

Java:

static int findMissing(int[] arr, int n) {
    int expected = n * (n + 1) / 2;
    int actual = 0;
    for (int val : arr) actual += val;
    return expected - actual;
}

Pattern 4: Maximum subarray sum — Kadane’s algorithm

This is the most frequently tested advanced array pattern in placement coding rounds. The insight: extending a subarray is worthwhile only when the running sum is positive. When the running sum drops below the current element, start a fresh subarray from that element.

The algorithm maintains two variables:

  • currentMax: the best sum ending at the current index
  • globalMax: the best sum seen so far

At each index: currentMax = max(arr[i], currentMax + arr[i]), then update globalMax.

Worked example with [-2, 1, -3, 4, -1, 2, 1, -5, 4]: the maximum subarray is [4, -1, 2, 1] with sum 6. Step-by-step: after index 0 both values are −2; at index 1, currentMax resets to 1; it climbs to 4 at index 3, then to 6 at index 6, where globalMax locks in.

  • Time: O(n), Space: O(1)

C:

int maxSubarraySum(int arr[], int n) {
    int current = arr[0], global = arr[0];
    for (int i = 1; i < n; i++) {
        current = (arr[i] > current + arr[i]) ? arr[i] : current + arr[i];
        if (current > global) global = current;
    }
    return global;
}

C++:

int maxSubarraySum(int arr[], int n) {
    int current = arr[0], global = arr[0];
    for (int i = 1; i < n; i++) {
        current = max(arr[i], current + arr[i]);
        global = max(global, current);
    }
    return global;
}

Java:

static int maxSubarraySum(int[] arr) {
    int current = arr[0], global = arr[0];
    for (int i = 1; i < arr.length; i++) {
        current = Math.max(arr[i], current + arr[i]);
        global = Math.max(global, current);
    }
    return global;
}

Pattern 5: Check whether an array is sorted

Traverse once and compare each element with its neighbour. If any adjacent pair is out of ascending order, return false immediately and stop.

  • Time: O(n), Space: O(1)

C:

int isSorted(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1]) return 0;
    }
    return 1;
}

C++:

bool isSorted(int arr[], int n) {
    for (int i = 0; i < n - 1; i++) {
        if (arr[i] > arr[i + 1]) return false;
    }
    return true;
}

Java:

static boolean isSorted(int[] arr) {
    for (int i = 0; i < arr.length - 1; i++) {
        if (arr[i] > arr[i + 1]) return false;
    }
    return true;
}

Pattern 6: Move all zeros to the end

Use a write-pointer j initialised to 0. In a single forward pass, copy every non-zero element to position j and increment j. Once the pass completes, fill positions j through n-1 with zeros. The relative order of non-zero elements is preserved.

  • Time: O(n), Space: O(1)

C:

void moveZerosToEnd(int arr[], int n) {
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] != 0) arr[j++] = arr[i];
    }
    while (j < n) arr[j++] = 0;
}

C++:

void moveZerosToEnd(int arr[], int n) {
    int j = 0;
    for (int i = 0; i < n; i++) {
        if (arr[i] != 0) arr[j++] = arr[i];
    }
    while (j < n) arr[j++] = 0;
}

Java:

static void moveZerosToEnd(int[] arr) {
    int j = 0;
    for (int val : arr) {
        if (val != 0) arr[j++] = val;
    }
    while (j < arr.length) arr[j++] = 0;
}

Time complexity at a glance

PatternTimeSpace
Find min / maxO(n)O(1)
Reverse in placeO(n)O(1)
Missing number (sum formula)O(n)O(1)
Kadane’s algorithmO(n)O(1)
Check if sortedO(n)O(1)
Move zeros to endO(n)O(1)

All six run in O(n) time with O(1) space. That uniformity is not incidental: placement coding platforms set input sizes to 10^5 elements or more specifically to eliminate O(n^2) brute-force submissions. If a nested-loop solution comes to mind for any of these patterns, that is a signal to step back and look for the single-pass version.

When you submit, state the complexity aloud or in a comment. Interviewers ask. Automated feedback systems in AMCAT and TCS NQT often display it as a scoring criterion alongside correctness.

From arrays to broader DSA and AI

Arrays are one layer of the DSA stack that placement tests cover. For trees, stacks, queues, and linked lists alongside arrays, 20 most asked data structures interview questions is a compact reference for what companies ask across the full stack. String manipulation questions, such as the palindrome check pattern, appear with nearly the same frequency as array questions in AMCAT’s coding module, and are worth covering alongside these six patterns rather than separately.

There is also a less-obvious connection between these patterns and the AI tools entering the placement picture. Every LLM processes tokens stored in tensor arrays. Every search-ranking function returns a score array. Once you can write Kadane’s algorithm from scratch in under 10 minutes, the interesting challenge shifts from passing the screen to building with what you know. TinkerLLM (₹299/month) is where that transition happens: running prompts, chaining outputs, and assembling small tools on top of LLM APIs using the same array and list operations the six patterns above rely on.

Primary sources

Frequently asked questions

Which array problems appear most often in TCS NQT?

TCS NQT coding sections commonly include find min/max, Kadane's algorithm, the missing-number sum trick, and array reversal. Two-pointer and zero-shifting patterns also appear regularly in the practice question banks.

Do I need to know C, C++ and Java for placement tests?

Most online platforms let you choose your language. Java is the safest single choice because its syntax is explicit and avoids pointer pitfalls. Master one language well before extending to others.

What is Kadane's algorithm and how does it work?

Kadane's algorithm finds the maximum sum contiguous subarray in O(n) time. It maintains a running sum and resets to the current element whenever the running sum drops below it, updating a global maximum throughout.

How do I find a missing number in an array without extra space?

For an array of n-1 numbers from 1 to n, compute expected_sum = n*(n+1)/2, subtract the actual array sum, and the difference is the missing number. Time: O(n), space: O(1).

What time complexity do placement tests expect for array problems?

Aim for O(n) on single-pass problems and O(n log n) for sort-based solutions. Avoid O(n^2) brute-force approaches — automated judges time out on large inputs designed to catch quadratic solutions.

Build AI projects

A self-paced playground for building with LLMs.

TinkerLLM is FACE Prep's sister property. A guided environment for shipping real LLM applications, the kind of project that earns a paragraph on your resume, not a line.

Try TinkerLLM (₹299 launch)
Free AI Roadmap PDF