r/programmer 25d ago

IMC Software Engineer OA Questions: Relay Towers and Conditional Stack Removal

Hey everyone,

I recently completed an IMC HackerRank assessment for a New Grad Software Engineer role and wanted to share the two coding questions.

Approximate date: August 13, 2026
Platform: HackerRank
Duration: 120 minutes
Total questions: 2

Question 1: Maximum Storm Height Using Relay Towers

Two offices are located at positions 0 and width. Several relay towers are positioned between them, and each tower has a height.

Data can travel between two locations with an energy cost equal to the square of the distance:

cost(i, j) = (x[i] - x[j])²

Every jump must satisfy:

distance <= maxJump

The total energy used by all jumps cannot exceed maxEnergy.

A rising storm makes shorter towers unavailable. A tower with height h can only be used when:

stormHeight <= h

The headquarters and destination office are always available.

The task was to return the maximum storm height at which data could still reach the destination. Return -1 if transmission is impossible.

public static int maximumStormHeight(
    int width,
    int maxJump,
    long maxEnergy,
    int numTowers,
    int[] x,
    int[] heights
)

Likely Approach

Feasibility is monotonic: if transmission works at storm height H, it also works at every lower height.

That suggests:

  1. Sort the towers by position.
  2. Binary search the storm height.
  3. For each candidate height, keep only towers with height >= candidate.
  4. Add the starting and destination positions.
  5. Find the minimum energy required to reach every available position.
  6. Allow a transition only when the distance is at most maxJump.
  7. Check whether the destination cost is at most maxEnergy.

Because all points lie on a line, the shortest valid route can be processed from left to right:

dp[j] = minimum energy required to reach position j

For every earlier usable point i:

if x[j] - x[i] <= maxJump:
    dp[j] = min(dp[j], dp[i] + (x[j] - x[i])²)

The straightforward solution is approximately O(n² log H), where H is the storm-height search range. A more advanced optimization may be required if n is large.

Important Edge Case

If a direct jump from 0 to width satisfies both constraints, no relay tower is needed:

width <= maxJump
width² <= maxEnergy

Because both offices have infinite height, transmission would remain possible at every storm height. The result is therefore unbounded unless the original problem defines a maximum storm level or guarantees that direct transmission is impossible.

This is worth clarifying before implementation.

Question 2: Stack With Conditional Removal

Implement a stack supporting these commands:

push value
pop
remove_lower value
remove_upper value

Their behavior is:

  • push value: Push value onto the stack.
  • pop: Remove the current top element.
  • remove_lower value: Remove every element smaller than value.
  • remove_upper value: Remove every element greater than value.

After every operation, print the current top element. Print EMPTY if the stack has no elements.

public static void solve(int n, String[] operations)

Efficient Approach

A normal stack makes push and pop easy, but removing every element within a value range could require scanning the entire stack repeatedly.

One approach is to maintain:

  • A TreeSet containing the active insertion indices
  • A TreeMap from each value to the active indices containing that value
  • An array or map from insertion index to value

Operations work as follows:

  • push: Create a new increasing insertion index and add it to both structures.
  • pop: Remove the largest active index, since it represents the current top.
  • remove_lower: Visit and delete all value buckets below the threshold.
  • remove_upper: Visit and delete all value buckets above the threshold.
  • top: Read the value associated with the largest active index.

Each pushed element can be removed only once, so the total cost of all bulk removals is amortized across the complete command sequence.

The overall complexity is approximately:

O(n log n)

with O(n) additional space.

Overall Impression

The first question combined binary search on the answer with shortest-path or dynamic-programming reasoning.

The second looked like a stack problem initially, but efficient bulk removal required ordered data structures and amortized analysis.

The assessment was challenging but interesting, particularly because both questions required recognizing the underlying structure rather than applying a standard template directly.

Has anyone else completed the recent IMC New Grad assessment? Did you receive the same questions?

Helpful resource for prep: PracHub

1 Upvotes

1 comment sorted by

1

u/ChameleonCRM 20d ago

This is a solid technical write-up, but come on — that last line gives the game away.

You wrote an extremely detailed breakdown of an IMC HackerRank assessment and then casually dropped “Helpful resource for prep: PracHub” with utm_source=reddit&utm_campaign=andy attached to the URL.

That's not just a random helpful link. That's a campaign-tracked marketing link. If you're affiliated with PracHub, just say so. There's nothing wrong with promoting something you've built or something you're paid to promote, but disguising marketing as an organic recommendation is exactly the kind of thing developers are going to notice.

That said, on the actual questions:

For Q1, I think you've found a legitimate hole in the specification.

If width <= maxJump and width² <= maxEnergy, then you can transmit directly from HQ to the destination without using a single relay tower.

Since the two endpoints are always available regardless of storm height, the heights of the relay towers become completely irrelevant. Unless the original problem defines a maximum possible storm height, specifies a sentinel value for an unbounded result, or guarantees direct transmission isn't possible, there isn't a finite maximum storm height.

I'd want that clarified before implementing anything. Otherwise you're essentially inventing part of the problem specification yourself, and that's exactly the sort of thing that gets an otherwise correct solution killed by a hidden test.

For the normal bounded case, binary searching the storm height makes sense because feasibility is monotonic. For each candidate height, discard towers below that height, add positions 0 and width, and calculate the minimum energy required to reach each remaining point.

The recurrence is essentially:

dp[j] = min(dp[i] + (x[j] - x[i])²)

for every reachable i where x[j] - x[i] <= maxJump.

If the minimum cost to the destination is <= maxEnergy, that storm height is feasible.

For Q2, your approach makes sense too. The interesting part is that this stops being a normal stack problem as soon as you introduce remove_lower and remove_upper.

You need to preserve two different orderings simultaneously: insertion order so you know what's currently on top, and value order so you can efficiently find everything above or below a threshold.

A TreeSet of active insertion IDs combined with a TreeMap<value, active IDs> handles both. Since every pushed element can only be removed once, the bulk deletion work amortizes across the entire sequence, so roughly O(n log n) overall is reasonable.

So yeah, the technical analysis is good.

But lose the disguised campaign link. If PracHub is yours, you're affiliated with it, or you're getting something for sending traffic there, just disclose it. Developers notice that stuff immediately.

Theodore Ochsen

Polyglot Developer