In: Computer Science
Question 1) In a circular array implemented in Python: blockCapacity=100, dataStartIndex=88, dataCount=43. What is the index of the last data item?
Answer:
Consider dynamic arrays with geometric expansion and no shrinking. Match operations to their complexity.
Always choose the most informative answer. For instance, if an operation is O(1) and O(n) choose O(1); if an operation is O(1) and O(1) amortized, choose O(1).
Options to choose from are: O(1), O(1) amortized or O(n)
addFirst :
len (length/size) :
deleteLast :
deleteFirst :
addLast (append) :
Data will start from index 88 and every next available space is filled with data. When end of array is reached, start from starting element. Here, started from 88 and went till 99, so total of 12 elements here. After that start from index 0 and fill the leftover data. Leftover data count is 31. So, starting from index 0, index 30 will have the last data item.
Python code:
n = int(input("Enter the blockSize: "))
a = [0]*n
start = int(input("Enter the startIndex: "))
data = int(input("enter the data count: "))
last = -1
for i in range(data):
index = (start + i)%n
a[index] = 1
last = index
print("Index of last data item is",last)
Output: