VOOZH about

URL: https://www.geeksforgeeks.org/dsa/create-customized-data-structure-evaluates-functions-o1/

⇱ Create a customized data structure which evaluates functions in O(1) - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Create a customized data structure which evaluates functions in O(1)

Last Updated : 5 Jul, 2025

Create a customized data structure such that it has functions :- 

  • GetLastElement(); 
  • RemoveLastElement(); 
  • AddElement() 
  • GetMin()

All the functions should be of O(1)

Question Source : amazon interview questions

Approach :

  1. create a custom stack of type structure with two elements, (element, min_till_now) 
  2. implement the functions on this custom data type 

Implementation:


Output
5 inserted successfully
7 inserted successfully
3 inserted successfully
min element :: 3
removed successfully
2 inserted successfully
9 inserted successfully
Last element :: 9
0 inserted successfully
min element :: 0
removed successfully
11 inserted successfully
min element :: 2

Time complexity: Each function runs in O(1)
Auxiliary space: O(n) for stack

Comment
Article Tags:
Article Tags: