Java Coding Problem - Ride-Share Dynamic Pricing
Brute force and optimization...
📢 I started another publication that focuses on SQL and Database & covers Daily SQL question. If it interests you Consider subscribing
Question
You are developing the core logic for a ride-sharing platform’s dynamic pricing engine. The city is segmented into N zones, and each zone i is constantly analyzed to determine its immediate Imbalance Factor (demand-to-supply ratio).
You are given an array of floats imbalanceFactors, where imbalanceFactors[i] represents the current demand/supply status for Zone i.
The core task is to calculate the Equilibrium Pressure Multiplier EPM for every zone. The EPM for Zone i is defined as the compounded (multiplicative) effect of all the other zones in the city, excluding Zone i.
Constraints:
No Division: You must not use the division operation in your algorithm, as dividing by a factor of 0 (zero available drivers in a zone) would cause runtime errors in the real system.
Example:
Input: imbalanceFactors = [1.5, 1.0, 2.0, 0.5]
Output: epm = [1.0, 1.5, 0.75, 3.0]Explanation:
epm[0] (Factor 1.5): 1.0 * 2.0 * 0.5 = 1.0
epm[1] (Factor 1.0): 1.5 * 2.0 * 0.5 = 1.5
epm[2] (Factor 2.0): 1.5 * 1.0 * 0.5 = 0.75
epm[3] (Factor 0.5): 1.5 * 1.0 * 2.0 = 3.0📢 Get actionable Java/Spring Boot insights every week, from practical code tips to real-world use-case based interview questions.
Join 6700+ subscribers and level up your Spring & backend skills with hand-crafted content, no fluff.
First 100 paid subscribers will get the annual membership at $50/year forever that is ~ $4/mo ( 71 already converted to paid, 29 remaining)
Solution
For every index i, we should compute the product of all elements except i.
So for each position:
Loop through the entire array
Skip the current index
Multiply everything else
Iteration 1 → i = 0
Skip index 0:
product = 1.0 × 2.0 × 0.5 = 1.0Complexity
Time: O(n²)
Space: O(1)
What optimization we can make in this approach?
Main issue with this approach is , we are recomputing overlapping part again and again.
Example:
For
i = 2: multiply[1.5, 1.0, 0.5]For
i = 3: multiply[1.5, 1.0, 2.0]
The prefix [1.5, 1.0] is reused
Optimized Approach (Prefix + Suffix):
We should break computation into:
Left product × Right product
For each index i:
EPM[i] = (product of elements before i) × (product of elements after i)Complexity
Time: O(n) (Two linear passes)
Space: O(1) (excluding output)
Thats all for this week friends! Thanks for reading this far. If you liked it please share with your network.
Happy Coding 🚀
Suraj
Subscribe | Sponsor us | LinkedIn | Twitter




