Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to retrieve the front element of the deque.
DSA C++
deque<int> dq;
// Assume dq has some elements
int frontElement = dq.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_front() when you want to access the element without removing it.
Using back() instead of front() to get the front element.
✗ Incorrect
The front() function returns the element at the front without removing it. pop_front() removes it.
2fill in blank
mediumComplete the code to remove the last element from the deque.
DSA C++
deque<int> dq;
// Assume dq has some elements
dq.[1]();Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_front() instead of pop_back() to remove the last element.
Using back() which only accesses the last element but does not remove it.
✗ Incorrect
pop_back() removes the last element from the deque.
3fill in blank
hardFix the error in the code to correctly remove and print the front element of the deque.
DSA C++
deque<int> dq = {10, 20, 30};
int x = dq.[1]();
dq.pop_front();
cout << x << endl;Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_front() to get the element which causes a compilation error.
Using back() instead of front() to get the front element.
✗ Incorrect
front() returns the front element; pop_front() removes it. Using pop_front() to get the element causes error.
4fill in blank
hardFill both blanks to correctly remove and print the last element of the deque.
DSA C++
deque<int> dq = {5, 15, 25};
int y = dq.[1]();
dq.[2]();
cout << y << endl;Attempts:
3 left
💡 Hint
Common Mistakes
Using front() or pop_front() instead of back() and pop_back().
Removing the element before accessing it.
✗ Incorrect
back() returns the last element; pop_back() removes it.
5fill in blank
hardFill all three blanks to remove the front element, add a new element at the back, and print the new back element.
DSA C++
deque<int> dq = {1, 2, 3};
dq.[1]();
dq.[2](4);
int z = dq.[3]();
cout << z << endl;Attempts:
3 left
💡 Hint
Common Mistakes
Using pop_back() instead of pop_front() to remove front element.
Using front() instead of back() to get last element.
✗ Incorrect
pop_front() removes the front element; push_back(4) adds 4 at the back; back() returns the last element.
