Prepin
Log in

Create a custom data structure that supports get() and setall() operations with a time complexity of O(1).

Approach / Explanation

This inquiry focuses on developing a custom data structure that supports two operations: get() and setall() , both of which should have a time complexity of O(1). The get() operation is responsible for fetching the value of a specific element from the data structure, while the setall() operation assigns a uniform value to all elements. The key challenge is to guarantee that both operations are executed in constant time, meaning their execution duration remains unaffected by the size of the data structure. This type of question is frequently encountered in interviews, as it evaluates your understanding of data structures, time and space complexity, as well as your problem-solving creativity. To effectively address this question, consider the following points: Outline the data structures you intend to utilize and explain their appropriateness. Elaborate on how you will implement the get() and setall() operations within your selected data structure. Clarify how these operations can be performed with O(1) time complexity.

Suggested Answer

I would create a custom data structure utilizing a Hashmap along with two additional variables. The Hashmap would hold the individual elements. One variable, which I will refer to as lastSetAll , would store the value assigned during the last setall() operation. The second variable, timeStamp , would track the timing of the most recent setall() operation. The get() function would fetch the value of an element from the hashmap at the specified index. If no value exists at that index, or if the timestamp of the setall() operation is more recent than that of the value at this index, it will return the value of the lastSetAll variable. The setall() function would simply update the lastSetAll variable with a new value and refresh the timeStamp , without modifying the entire hashmap. This results in both get() and setall() having a time complexity of O(1).

Alternative Answer

Here’s a Python code snippet that outlines my method using a dictionary along with two variables, lastSetAll and timeStamp . class DataStructure : def __init__ ( self ) : self . data = { } self . lastSetAll = 0 self . timeStamp = 0 def get ( self , index ) : if index not in self . data or self . data [ index ] [ 1 ] < self . timeStamp : return self . lastSetAll else : return self . data [ index ] [ 0 ] def setall ( self , value ) : self . lastSetAll = value self . timeStamp += 1 In the get() method, we verify whether the index is absent from the dictionary or if the setAll operation occurred after the value was set at that index. If either condition is met, we return the lastSetAll value, which represents the most recent valid value for that index; otherwise, we return the actual value. For the setall() method, we simply update the lastSetAll and timeStamp , ensuring that this operation runs in constant time.

Question Details

Difficulty & Category

Medium

Product

For CandidatesFor RecruitersCompaniesBrowse JobsInterview QuestionsDeep ProfileSkill AssessmentOpportunity Matching
Prepin.ai

© 2026 Prepin LLC · Philadelphia, PA · All rights reserved.