Coding Questions for Placement: Prepare for Tech Interviews

Published: 16 Dec 2024
Reading Time: 9 min read

Cracking the coding round is a part of placement toward securing a job. As companies seek to hire individuals who know coding which is more important than ever. In this article, we will explore the significance of coding questions for placements, the types of coding questions asked, and how to prepare effectively for your placement.

Table of Contents

Importance of Coding in Placements

The coding round in placements cannot be ignored. Coding assessments are one of the primary methods for evaluating candidates' problem-solving skills, logical thinking, and technical knowledge. Most companies prefer candidates who excel in coding round questions for placement because it demonstrates their ability to work under pressure and their proficiency in programming concepts.

The coding problems are tested for job placement, and practicing regularly ensures that you can tackle a wide range of problems. These coding tests are designed to test your understanding of basic programming questions for placement, data structures and algorithms (DSA), and even advanced topics like dynamic programming and graph theory.

Types of Coding Questions for 2025

In 2025, the types of coding questions asked by companies during technical interviews are evolving with the increasing demand for problem-solving. Here's a breakdown of the types of coding questions asked in companies:

Google Interview Coding Questions

Facebook Interview Coding Questions

Amazon Interview Coding Questions

Microsoft Interview Coding Questions

Adobe Interview Coding Questions

Oracle Interview Coding Questions

Walmart Interview Coding Questions

Cisco Interview Coding Questions

Qualcomm Interview Coding Questions

IBM Interview Coding Questions

Accenture Interview Coding Questions

Most Asked Coding Questions for Placement Hiring 2025

Candidates can expect these types of coding questions for their placement hiring. Here are the top 15 coding questions with solutions in C++:

Problem 1: Identify the missing number in an array with n-1 numbers

Problem Statement: Find the missing number in an array containing n-1 numbers from a sequence of n numbers.

Solution:

#include <iostream>
#include <vector>
using namespace std;

int findMissingNumber(vector<int>& nums) {
    int n = nums.size() + 1;
    int total_sum = n * (n + 1) / 2;
    int array_sum = 0;
    for (int num : nums) {
        array_sum += num;
    }
    return total_sum - array_sum;
}

int main() {
    vector<int> nums = {1, 2, 4, 6, 3, 7, 8};
    cout << "Missing Number: " << findMissingNumber(nums) << endl;
    return 0;
}

Output:

Missing Number: 5

Problem 2: Given an array of integers and a target number, find the two numbers that sum up to the target

Problem Statement: Find two indices in an array where the values sum to a given target.

Solution:

#include <iostream>
#include <unordered_map>
#include <vector>
using namespace std;

vector<int> twoSum(const vector<int>& nums, int target) {
    unordered_map<int, int> map;
    for (int i = 0; i < nums.size(); i++) {
        int complement = target - nums[i];
        if (map.find(complement) != map.end()) {
            return {map[complement], i};
        }
        map[nums[i]] = i;
    }
    return {};
}

int main() {
    vector<int> nums = {2, 7, 11, 15};
    int target = 9;
    vector<int> result = twoSum(nums, target);

    cout << "Indices: " << result[0] << ", " << result[1] << endl;
    return 0;
}

Output:

Indices: 0, 1

Problem 3: Calculate the longest substring without repeating characters

Problem Statement: Find the length of the longest substring without repeating characters.

Solution:

#include <iostream>
#include <unordered_map>
#include <string>
using namespace std;

int lengthOfLongestSubstring(string s) {
    unordered_map<char, int> map;
    int left = 0, max_len = 0;
    for (int right = 0; right < s.length(); ++right) {
        if (map.find(s[right]) != map.end()) {
            left = max(left, map[s[right]] + 1);
        }
        map[s[right]] = right;
        max_len = max(max_len, right - left + 1);
    }
    return max_len;
}

int main() {
    string s = "abcabcbb";
    cout << "Length of Longest Substring: " << lengthOfLongestSubstring(s) << endl;
    return 0;
}

Output:

Length of Longest Substring: 3

Problem 4: Merge two sorted arrays into a single sorted array

Problem Statement: Merge two sorted arrays into one sorted array.

Solution:

#include <stdio.h>

void mergeArrays(int arr1[], int arr2[], int n1, int n2, int result[]) {
    int i = 0, j = 0, k = 0;
    while (i < n1 && j < n2) {
        if (arr1[i] < arr2[j]) {
            result[k++] = arr1[i++];
        } else {
            result[k++] = arr2[j++];
        }
    }
    while (i < n1) {
        result[k++] = arr1[i++];
    }
    while (j < n2) {
        result[k++] = arr2[j++];
    }
}

int main() {
    int arr1[] = {1, 3, 5, 7};
    int arr2[] = {2, 4, 6, 8};
    int result[8];

    mergeArrays(arr1, arr2, 4, 4, result);

    printf("Merged Array: ");
    for (int i = 0; i < 8; i++) {
        printf("%d ", result[i]);
    }
    printf("\n");
    return 0;
}

Output:

Merged Array: 1 2 3 4 5 6 7 8

Problem 5: Implement binary search on a sorted array

Problem Statement: Implement binary search to find a target element in a sorted array.

Solution:

#include <iostream>
#include <vector>
using namespace std;

int binarySearch(const vector<int>& arr, int target) {
    int left = 0, right = arr.size() - 1;
    while (left <= right) {
        int mid = left + (right - left) / 2;
        if (arr[mid] == target) {
            return mid;
        }
        if (arr[mid] < target) {
            left = mid + 1;
        } else {
            right = mid - 1;
        }
    }
    return -1;
}

int main() {
    vector<int> arr = {1, 3, 5, 7, 9, 11, 13};
    int target = 7;
    int result = binarySearch(arr, target);

    if (result != -1) {
        cout << "Element found at index " << result << endl;
    } else {
        cout << "Element not found" << endl;
    }
    return 0;
}

Output:

Element found at index 3

Problem 6: Find the largest sum of non-adjacent numbers in an array

Problem Statement: Find the maximum sum of non-adjacent elements in an array.

Solution:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int largestSumNonAdjacent(const vector<int>& nums) {
    int include = 0, exclude = 0;
    for (int num : nums) {
        int new_exclude = max(include, exclude);
        include = exclude + num;
        exclude = new_exclude;
    }
    return max(include, exclude);
}

int main() {
    vector<int> nums = {2, 4, 6, 2, 5};
    cout << "Largest Sum of Non-Adjacent Numbers: " << largestSumNonAdjacent(nums) << endl;
    return 0;
}

Output:

Largest Sum of Non-Adjacent Numbers: 10

Problem 7: Find the Kth largest element in an unsorted array

Problem Statement: Find the Kth largest element in an unsorted array.

Solution:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int findKthLargest(vector<int>& nums, int k) {
    priority_queue<int, vector<int>, greater<int>> minHeap;
    for (int num : nums) {
        minHeap.push(num);
        if (minHeap.size() > k) {
            minHeap.pop();
        }
    }
    return minHeap.top();
}

int main() {
    vector<int> nums = {3, 2, 1, 5, 6, 4};
    int k = 2;
    cout << "Kth largest element is: " << findKthLargest(nums, k) << endl;
    return 0;
}

Output:

Kth largest element is: 5

Problem 8: Count the number of inversions in an array

Problem Statement: Count the number of inversions in an array, where an inversion is a pair (i, j) such that i < j and arr[i] > arr[j].

Solution:

#include <iostream>
#include <vector>
using namespace std;

int mergeAndCount(vector<int>& arr, vector<int>& temp, int left, int right) {
    if (left == right) return 0;
    int mid = left + (right - left) / 2;
    int inv_count = mergeAndCount(arr, temp, left, mid);
    inv_count += mergeAndCount(arr, temp, mid + 1, right);
    inv_count += merge(arr, temp, left, mid, right);
    return inv_count;
}

int merge(vector<int>& arr, vector<int>& temp, int left, int mid, int right) {
    int i = left, j = mid + 1, k = left, inv_count = 0;
    while (i <= mid && j <= right) {
        if (arr[i] <= arr[j]) {
            temp[k++] = arr[i++];
        } else {
            temp[k++] = arr[j++];
            inv_count += (mid - i + 1);
        }
    }
    while (i <= mid) temp[k++] = arr[i++];
    while (j <= right) temp[k++] = arr[j++];
    for (i = left; i <= right; i++) arr[i] = temp[i];
    return inv_count;
}

int main() {
    vector<int> arr = {1, 20, 6, 4, 5};
    vector<int> temp(arr.size());
    cout << "Number of inversions: " << mergeAndCount(arr, temp, 0, arr.size() - 1) << endl;
    return 0;
}

Output:

Number of inversions: 5

Problem 9: Find a peak element in an array

Problem Statement: Find a peak element in an array. An element is a peak if it is greater than or equal to its neighbours.

Solution:

#include <iostream>
#include <vector>
using namespace std;

int findPeakElement(vector<int>& nums) {
    int left = 0, right = nums.size() - 1;
    while (left < right) {
        int mid = left + (right - left) / 2;
        if (nums[mid] > nums[mid + 1]) {
            right = mid;
        } else {
            left = mid + 1;
        }
    }
    return left;
}

int main() {
    vector<int> nums = {1, 2, 3, 1};
    cout << "Peak Element: " << findPeakElement(nums) << endl;
    return 0;
}

Output:

Peak Element: 2

Problem 10: Given two strings s and p, find all the start indices of p's anagrams in s

Problem Statement: Find all starting indices of anagrams of string p in string s.

Solution:

#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

vector<int> findAnagrams(string s, string p) {
    vector<int> result;
    if (s.size() < p.size()) return result;

    unordered_map<char, int> p_map, s_map;

    for (char c : p) p_map[c]++;

    for (int i = 0; i < p.size(); ++i) s_map[s[i]]++;

    if (p_map == s_map) result.push_back(0);

    for (int i = p.size(); i < s.size(); ++i) {
        s_map[s[i]]++;
        s_map[s[i - p.size()]]--;

        if (s_map[s[i - p.size()]] == 0) {
            s_map.erase(s[i - p.size()]);
        }

        if (p_map == s_map) result.push_back(i - p.size() + 1);
    }

    return result;
}

int main() {
    string s = "cbaebabacd", p = "abc";
    vector<int> result = findAnagrams(s, p);

    cout << "Anagram Indices: ";
    for (int index : result) {
        cout << index << " ";
    }
    cout << endl;
    return 0;
}

Output:

Anagram Indices: 0 6

Problem 11: Rotting Oranges - Find minimum time for all oranges to rot

Problem Statement: In a grid of oranges, some are rotten and some are fresh. Each minute, any fresh orange that is adjacent to a rotten one becomes rotten. Return the minimum number of minutes that must elapse before no fresh oranges remain.

Solution:

#include <iostream>
#include <vector>
#include <queue>
using namespace std;

int orangesRotting(vector<vector<int>>& grid) {
    int fresh = 0, minutes = 0;
    queue<pair<int, int>> q;

    for (int i = 0; i < grid.size(); ++i) {
        for (int j = 0; j < grid[0].size(); ++j) {
            if (grid[i][j] == 1) fresh++;
            if (grid[i][j] == 2) q.push({i, j});
        }
    }
    if (fresh == 0) return 0;

    vector<vector<int>> directions = {{-1, 0}, {1, 0}, {0, -1}, {0, 1}};

    while (!q.empty()) {
        int size = q.size();
        bool rot = false;

        for (int i = 0; i < size; ++i) {
            auto [x, y] = q.front();
            q.pop();

            for (auto& dir : directions) {
                int nx = x + dir[0], ny = y + dir[1];
                if (nx >= 0 && ny >= 0 && nx < grid.size() && ny < grid[0].size() && grid[nx][ny] == 1) {
                    grid[nx][ny] = 2;
                    fresh--;
                    rot = true;
                    q.push({nx, ny});
                }
            }
        }

        if (rot) minutes++;
    }

    return fresh == 0 ? minutes : -1;
}

int main() {
    vector<vector<int>> grid = {{2, 1, 1}, {1, 1, 0}, {0, 1, 1}};
    cout << "Minutes to rot all oranges: " << orangesRotting(grid) << endl;
    return 0;
}

Output:

Minutes to rot all oranges: 4

Problem 12: Check whether a string has balanced parentheses

Problem Statement: Validate if a string containing parentheses is balanced.

Solution:

#include <iostream>
#include <stack>
using namespace std;

bool isValid(string s) {
    stack<char> stk;

    for (char c : s) {
        if (c == '(' || c == '{' || c == '[') {
            stk.push(c);
        } else {
            if (stk.empty()) return false;
            char top = stk.top();
            if ((c == ')' && top == '(') || (c == '}' && top == '{') || (c == ']' && top == '[')) {
                stk.pop();
            } else {
                return false;
            }
        }
    }

    return stk.empty();
}

int main() {
    string s = "({[]})";
    cout << "Balanced Parentheses: " << isValid(s) << endl;
    return 0;
}

Output:

Balanced Parentheses: 1

Problem 13: Count the number of prime numbers less than a given number n

Problem Statement: Count prime numbers less than n using the Sieve of Eratosthenes.

Solution:

#include <iostream>
#include <vector>
using namespace std;

int countPrimes(int n) {
    if (n <= 2) return 0;
    vector<bool> isPrime(n, true);
    isPrime[0] = isPrime[1] = false;

    for (int i = 2; i * i < n; ++i) {
        if (isPrime[i]) {
            for (int j = i * i; j < n; j += i) {
                isPrime[j] = false;
            }
        }
    }

    int count = 0;
    for (int i = 2; i < n; ++i) {
        if (isPrime[i]) count++;
    }
    return count;
}

int main() {
    int n = 10;
    cout << "Number of primes less than " << n << ": " << countPrimes(n) << endl;
    return 0;
}

Output:

Number of primes less than 10: 4

Problem 14: Find the next lexicographical permutation of a given list of numbers

Problem Statement: Find the next permutation of a sequence of numbers in lexicographical order.

Solution:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void nextPermutation(vector<int>& nums) {
    int i = nums.size() - 2;
    while (i >= 0 && nums[i] >= nums[i + 1]) {
        --i;
    }
    if (i >= 0) {
        int j = nums.size() - 1;
        while (nums[j] <= nums[i]) {
            --j;
        }
        swap(nums[i], nums[j]);
    }
    reverse(nums.begin() + i + 1, nums.end());
}

int main() {
    vector<int> nums = {1, 2, 3};
    nextPermutation(nums);

    cout << "Next Permutation: ";
    for (int num : nums) {
        cout << num << " ";
    }
    cout << endl;
    return 0;
}

Output:

Next Permutation: 1 3 2

Problem 15: Print the nth Fibonacci number using dynamic programming

Problem Statement: Calculate the nth Fibonacci number using dynamic programming.

Solution:

#include <iostream>
#include <vector>
using namespace std;

int fib(int n) {
    if (n <= 1) return n;
    vector<int> dp(n + 1, 0);
    dp[1] = 1;

    for (int i = 2; i <= n; ++i) {
        dp[i] = dp[i - 1] + dp[i - 2];
    }
    return dp[n];
}

int main() {
    int n = 6;
    cout << "Fibonacci number: " << fib(n) << endl;
    return 0;
}

Output:

Fibonacci number: 8

Mock Interviews and Preparation Strategy

Mock interviews are essential for interview preparation, allowing you to practice real-world scenarios, improve communication, and boost confidence. Here's an effective strategy:

Preparation Timeline

Simulate Real Conditions

Seek Feedback

Practice Common Questions

Focus on Core Skills

Last-Minute Preparation

Mock interviews and strategic last-minute preparation will ensure you're ready and confident for the real interview.

Conclusion

In conclusion, learning coding is crucial for placement success, as it assesses problem-solving, logic, and technical skills. In 2025, companies like Google, Amazon, and Microsoft focuses on their specific needs, requiring candidates to understand their unique expectations. Preparing through mock interviews, coding practice, and focusing on common patterns is essential. Strategic mock interviews will greatly improve your chances of excelling in placement coding rounds.

Frequently Asked Questions

What are the common coding questions for placements?

Common coding questions for placement include concepts related to arrays, strings, sorting, searching, recursion, dynamic programming, and graphs.

How can I prepare for coding rounds in campus placements?

To prepare for campus placement coding questions, focus on mastering DSA questions for placement, practicing regularly, and using resources like Codechef and LeetCode.


Contact Information

NxtWave

Related Articles