Stack

Stack

7 questions · Fundamental Engineering

Practice

From the answer group below, select the correct answer to be inserted into blank in the description.

When the procedure proc1 is called, the output is “blank” in turn.

[Program]
○ proc1()
proc2()
proc3()
output "A, "
○ proc2()
output "B, "
proc3()
○ proc3()
output "C, "

Answer group

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 0.

The program implements a queue that has a fixed capacity and only accepts integers. If the queue is not full, the function enqueue appends the specified value to its end and returns true. Otherwise, it returns false. If the queue is not empty, the function dequeue removes an element from the queue and returns its value. Otherwise, it returns undefined. The figure illustrates the working of a queue with capacity to hold up to seven integers. Initially, the queue contains five integers, with front, rear, and count set to 3, 1, and 5, respectively. Then, enqueue(51) and dequeue() are called in that order.

index0123456
item34undefinedundefined17132719

enqueue(51) is called.

index0123456
item3451undefined17132719

dequeue() is called.

index0123456
item3451undefinedundefined132719

Figure Queue with capacity of seven

[Program]
global: integer: capacity ← 7 /* Capacity of the queue */
global: integer []: item ← {7 undefined}
global: integer: count ← 0 /* Number of elements in the queue */
global: integer: front ← 0 /* Index of the front element */
global: integer: rear ← 0 /* Index of the element next to the rear */
○ boolean: enqueue(integer: val)
if (count A)
item[rear] ← val
count ← count + 1
rear ← (rear + 1) mod capacity
return true
else
return false
endif
○ integer: dequeue()
integer val
if (count > 0)
val ← item[front]
item[front] ← undefined
count ← count − 1
front ← B
return val
else
return undefined
endif

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

Stack stores data using first-in, last-out ordering. Here, the items stored in the stack are integer values, and the stack is controlled by the procedure push and the function pop. The procedure push adds an item given as an argument to the top of the stack, and the function pop removes the top item from the stack and returns it. The global variable stck is an array of 10 integers that stores the stack items, and the global variable tos is the pointer that points to the topmost item of the stack.

[Program]
global: integer []: stck ← {10 undefined}
global: integer: tos ← 0
○ push(integer: item)
if (tos = 10)
output "Stack is Full"
else
A
stck[tos] ← item
endif
○ integer: pop()
integer: item
if (tos < 1)
output "Stack is Empty"
return undefined
else
B
endif
return item

Answer group

OptionAB

From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.

The function reverse takes a string inputStr as a parameter and returns the reversed string. Here, the length of the string given to inputStr is 100 or less. In the program, areas outside of the arrays must not be referenced and the undefined value must not be appended to a string.

[Program]
global: character []: stack ← {100 undefined}
global: integer: sp ← 0
○ string: reverse(string: inputStr)
integer: n ← length of inputStr
integer: i
character: x, v
string: outputStr ← ""
for (increase i from 1 to n by 1)
x ← the i-th character of string inputStr
push(x)
endfor
while (sp ≠ A)
v ← pop()
append v to outputStr
endwhile
return outputStr
○ push(character: x)
sp ← sp + 1
stack[sp] ← x
○ character: pop()
character: retvar
B
return retvar

Answer group

OptionAB

From the answer group below, select the correct answer to be inserted into blank in the description.

This program performs operations on a priority queue.
A priority queue is a queue where the handled elements have a priority assigned to them, and the elements are extracted with the order of the highest priority first. The class PrioQueue represents a priority queue. The Figure shows an explanation of the class PrioQueue. Here, the priority is the integer value 1, 2, or 3, and the smaller the value the higher the priority.

When the procedure prioSched is called, the order of the output is blank.

ConstructorDescription
PrioQueue()Creates an empty priority queue.
MethodType of return valueDescription
enqueue(string: s,integer: prio)NoneAdds string s as an element to a priority queue with the priority prio.
dequeue()stringExtracts the element with the highest priority in a priority queue and returns it. If multiple elements with the highest priority exist, it extracts the element that was added first and returns it.
size()integerReturns the number of elements that are stored in a priority queue.

Figure Explanation of the class PrioQueue

[Program]
○ prioSched()
PrioQueue: prioQueue ← PrioQueue()
prioQueue.enqueue("E", 3)
prioQueue.enqueue("F", 2)
prioQueue.enqueue("G", 1)
prioQueue.enqueue("H", 1)
prioQueue.dequeue() /* The return value is ignored */
prioQueue.dequeue() /* The return value is ignored */
prioQueue.enqueue("I", 1)
prioQueue.enqueue("J", 1)
prioQueue.dequeue() /* The return value is ignored */
prioQueue.enqueue("K", 2)
prioQueue.enqueue("L", 3)
prioQueue.enqueue("M", 1)
while (prioQueue.size() is not equal to 0)
output prioQueue.dequeue()
endwhile

Answer group

From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.

The program implements a stack. The stack implementation only accepts positive integers. The function empty checks whether the stack is empty. The function full checks whether the stack is full. If the stack is not full, the function push pushes an element with a specified value onto the stack. If the stack is not empty, the function pop removes an element from the stack and returns its value. In the program, areas outside of the array must not be referenced.

[Program]
global: integer []: content
← {undefined, undefined, undefined, undefined}
global: integer: index ← 1
global: integer: max ← 4 /* max size of the stack */
○ boolean: empty()
if (index = 1)
return true
else
return false
endif
○ boolean: full()
if (A)
return true
else
return false
endif
○ boolean: push(integer: i)
if (not full())
content[index] ← i
index ← B
return true
else
return false
endif
○ integer: pop()
if (not empty())
index ← C
return content[index]
else
return -1
endif

Answer group

OptionABC

From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.

The function are_brackets_balanced checks for balanced brackets. It parses the given array of characters and when an opening bracket (“(”, “[”, “{”) is encountered, this is pushed onto the stack. When a closing bracket (“)”, “]”, “}”) is encountered, an element is popped from the stack and tested if it corresponds to the opening bracket. If the closing bracket matches its corresponding opening bracket, the process continues. Otherwise, it fails and the function returns false. After all characters have been processed, it returns false if any characters remain on the stack, otherwise it returns true. For simplicity, only brackets are considered as arguments to the function. The table shows examples of arguments provided to are_brackets_balanced and the return values.

Table Examples of arguments provided to the function are_brackets_balanced and the return values

Function callReturn value
are_brackets_balanced({"(", "{", "}", ")", "[", "]"})true
are_brackets_balanced({"(", "{", "}", "[", "]"})false
are_brackets_balanced({"(", "{", ")", "}", "[", "]"})false

The function are_brackets_balanced uses class Stack. The figure describes class Stack.

ConstructorDescription
Stack()Initialize a stack.
MethodReturn valueDescription
push(character: arg)NonePushes arg onto the stack.
pop()characterReturns the value popped from the stack.
isEmpty()booleanReturns true if the stack is empty.

Figure Class Stack

[Program]
global: character [][]: brackets ← {
{"(", ")"},
{"{", "}"},
{"[", "]"}
}
○ boolean: are_brackets_balanced(character[]: expr)
Stack: stack ← Stack()
character: c, stacked_bracket
for (c in expr)
if (is_opening_bracket(c))
stack.push(c)
else
if (stack.isEmpty())
return false
endif
stacked_bracket ← stack.pop()
if (get_closing_bracket(stacked_bracket) A)
return false
endif
endif
endfor
return B
○ boolean: is_opening_bracket(character: c)
character []: chars
for (chars in brackets)
if (chars[1] = c)
return true
endif
endfor
return false
○ character: get_closing_bracket(character: c)
character []: chars
for (chars in brackets)
if (chars[1] = c)
return C
endif
endfor
return undefined

Answer group

OptionABC