-
Notifications
You must be signed in to change notification settings - Fork 3
Stacks and Queues
Question 1. Queue with two stacks.
The main idea is to have two stacks, the first of which will be filled with data, and the second will be replenished from the first when it runs out of elements and elements will be taken from it to the output.
Question 2. Stack with max.
The solution to this problem is to have an additional stack containing the maximum elements in the order in which they were added to the main stack, then when the current maximum element is popped out, the previous maximum element will immediately take its place.
Question 3. Java generics.
It's because Java's arrays (unlike generics) contain, at runtime, information about its component type. So you must know the component type when you create the array. Since you don't know what T is at runtime, you can't create the array. If generic array creation were legal, then compiler generated casts would correct the program at compile time but it can fail at runtime, which violates the core fundamental system of generic types.