I came across this problem in an interview website. The problem asks for efficiently implement three stacks in a single array, such that no stack overflows until there is no
Make a HashMap with keys to the begin and end positions e.g. < "B1" , 0 >, <"E1" , n/3 >
for each Push(value) add a condition to check if position of Bx is previous to Ex or there is some other "By" in between. -- lets call it condition (2)
with above condition in mind, if above (2) is true // if B1 and E1 are in order { if ( S1.Push()), then E1 ++ ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }
if above (2) is false { if ( S1.Push()), then E1 -- ; else // condition of overflow , { start pushing at end of E2 or E3 (whichever has a space) and update E1 to be E2-- or E3-- ; } }