Question: IBM Online Assessment (OA) Coding Questions & Solutions | HackerRank Aug2025
1
Entering edit mode

Question 1: Find Maximum Bandwidths

Problem Statement:

A service provider manages n API endpoints, each serving different functionalities, numbered from 1 to n. Given m bandwidth units, allocate these among the endpoints.

The objective is to maximize the bandwidth allocated to the k-th endpoint while meeting the following conditions:

  • All bandwidth units must be allocated.

  • Each endpoint must have at least 1 unit of bandwidth allocated.

  • The absolute difference in bandwidth between any two adjacent endpoints cannot exceed 1.

Return the maximum possible bandwidth that can be allocated to the k-th endpoint satisfying all conditions.

Example:

n = 4

k = 3

m = 17

One optimal way to distribute m = 17 units of bandwidth among the n = 4 endpoints is [3, 4, 5, 5]. Here, the 3rd endpoint is allocated 5 units of bandwidth. Hence, 5 is the required answer.

 

Question 2: Substrings with No Repeating Characters

Problem Statement:

Given a string s, determine how many different substrings exist that have no repeating characters. Two substrings are considered different if they have different start or end indices.

Example:

s = "abac"

The substrings with no repeating characters are "a", "b", "a", "c", "ab", "ba", "ac", and "bac".

Note that "aba" and "abac" do not qualify because the character 'a' is repeated in them. Also, "a" and "a" both qualify because their start indices are different: s[0] and s[2]. There are 8 such substrings.

 

Sample Cases:

  • s = "bcada" output: 12

  • s = "abcd"  Output: 10

ADD COMMENTlink 22 days ago admin 1.9k
Entering edit mode
0

Solution -2 (Substrings with No Repeating Characters Solution)

Topics Involved / Prerequisites -: Sliding Window / Two Pointers , Frequency Arrays (Hash Maps)

Overview

Instead of checking every single possible substring one by one (which would take forever!), we can just stretch our frame to the right until we accidentally capture a duplicate character. When we do, we simply pull the left side of the frame in until the duplicate falls out. The magic trick? The size of the frame at any given point tells you exactly how many new valid substrings end at that character!

Approach

1. The Sliding Window

We use two pointers, l (left) and r (right), starting at the beginning of the string. We move r forward, adding characters to our current "window" by tracking their counts in a frequency array.

2. Shrinking and Counting

If adding s[r] causes a duplicate, we advance our l pointer, removing characters from our frequency array until the duplicate is gone. Once the window is valid again, the number of valid substrings ending at r is simply the mathematical length of the window: r - l + 1. We add this to our total answer.

#include <iostream>
#include <vector>
#include <string>

using namespace std;

long long countSubstrings(string s) {
    vector<int> cnt(256, 0); 
    long long ans = 0;
    int l = 0;
    
    for (int r = 0; r < s.length(); r++) {
        cnt[s[r]]++; 
        
        while (cnt[s[r]] > 1) {
            cnt[s[l]]--;
            l++;
        }
  
        ans += (r - l + 1); 
    }
    
    return ans;
}

int main() {
    // Sample Case 1
    cout << "Case 1: " << countSubstrings("abac") << "\n"; // Output: 8
    
    // Sample Case 2
    cout << "Case 2: " << countSubstrings("bcada") << "\n"; // Output: 12
    
    // Sample Case 3
    cout << "Case 3: " << countSubstrings("abcd") << "\n"; // Output: 10
    
    return 0;
}

Time Complexity

  • Time: O(N) - Both the l and r pointers only move forward, meaning we visit each character at most twice.

  • Space: O(1) - We use a fixed-size array of 256 integers to represent ASCII character counts.

ADD REPLYlink 22 days ago
Rohit
• 40
0
Entering edit mode

Solution- 1 (Find Maximum Bandwidths Solution)

Topics Involved / Prerequisites -: Binary Search on Answer,Greedy Math , Arithmetic Progressions

Overview

This is a classic optimization problem masquerading as an array puzzle! Think of a situation where you want to build the tallest possible tower of blocks at a specific spot. Because the height difference between adjacent towers can't be more than 1, making your target tower taller forces the surrounding towers to be taller too, almost like a pyramid! Since we have a fixed number of blocks (bandwidth m), we can just "guess" the height of our target tower and use math to see if we have enough blocks to build the supporting pyramid around it.

Approach

1. Binary Search the Answer

The lowest possible bandwidth for the k-th endpoint is 1, and the absolute maximum is m. We can use binary search to guess a bandwidth value X between 1 and m. If X is valid, we try a higher number. If it isn't, we try a lower number.

2. Calculating the Minimum Pyramid

For a guessed height X at index k, the values to its left and right must step down by exactly 1 until they hit 1. We can calculate the exact sum of these left and right sides using the sum formula for an Arithmetic Progression in O(1) time! If Left_Sum + Right_Sum + X is <= m, then our guess is possible.

#include <iostream>
#include <vector>
#include <algorithm>

using namespace std;

// Helper to calculate required sum for one side of the pyramid
long long getReqSum(long long val, int len) {
    if (len == 0) return 0;
    if (val > len) {
  
        return 1LL * len * (val - 1 + val - len) / 2;
    } else {
        // Drops to 1, then stays at 1 for the remaining length
        long long ones = len - val + 1;
        return 1LL * val * (val - 1) / 2 + ones;
    }
}

long long maxBandwidth(int n, int k, long long m) {
    long long low = 1, high = m, ans = 1;
    
    while (low <= high) {
        long long mid = low + (high - low) / 2;
        
        long long lSum = getReqSum(mid, k - 1);
        long long rSum = getReqSum(mid, n - k);
        
        // If we have enough bandwidth, try a bigger height
        if (lSum + rSum + mid <= m) {
            ans = mid;
            low = mid + 1;
        } else {
            high = mid - 1;
        }
    }
    return ans;
}

int main() {
    // Sample Case 1
    cout << "Case 1: " << maxBandwidth(4, 3, 17) << "\n"; // Output: 5
    
    return 0;
}

Time Complexity

  • Time: O(log M) - The binary search halves the search space of M each time, and our math check inside the loop takes O(1) constant time!

  • Space: O(1) - Only a few variables are used.

ADD COMMENTlink 22 days ago Rohit • 40

Login before adding your answer.

Similar Posts
Loading Similar Posts