NatWest interview prep
5 practice questions on the topics candidates reported from NatWest interviews, with answers. The questions are ours; each one links to the interview report that names its topic.
Practise them as a timed round11 roles open at NatWest Group
DSA 2
-
In the rotting oranges problem, each minute every rotten orange rots its fresh neighbours. Which approach finds the minutes until no fresh orange is left?
- A depth-first search from one rotten orange
- Sorting the grid
- A multi-source BFS that starts from all rotten oranges at once, one level per minute
- Union-find over the fresh oranges
Show the answer
C. A multi-source BFS that starts from all rotten oranges at once, one level per minute
All rotten oranges spread at the same time, so they all start in the queue together and each BFS level is one minute. If a fresh orange is never reached, the answer is -1.
This topic is named in a candidate’s interview report: Software engineer on-campus interview, 2023, on GeeksforGeeks.
-
In this grid 2 is rotten, 1 is fresh and 0 is empty. Each minute every rotten orange rots the fresh oranges above, below, left and right of it. After how many minutes are no fresh oranges left?
2 1 1 1 1 0 0 1 1- 3
- 4
- 5
- -1, some orange never rots
Show the answer
B. 4
Minute 1 rots the two neighbours of the corner, minute 2 the next ring, minute 3 the middle of the bottom row, and minute 4 the bottom-right orange.
This topic is named in a candidate’s interview report: Software engineer on-campus interview, 2023, on GeeksforGeeks.
C and C++ 3
-
What does this print, and what would it print if the word virtual were removed?
#include <iostream> struct Shape { virtual const char* name() { return "shape"; } }; struct Circle : Shape { const char* name() override { return "circle"; } }; int main() { Shape* s = new Circle(); std::cout << s->name(); }- circle; without virtual it would print shape
- shape; without virtual it would print circle
- circle in both cases
- shape in both cases
Show the answer
A. circle; without virtual it would print shape
A virtual call is resolved at run time from the object's real type, so Circle::name runs. Without virtual (and with override removed, which would otherwise not compile), the call is bound at compile time from the pointer's type, Shape, and prints shape.
This topic is named in a candidate’s interview report: Software engineer on-campus interview, 2023, on GeeksforGeeks.
-
B and C both inherit from A, and D inherits from both B and C. How does C++ stop a D object from holding two copies of A?
- Make A an abstract class
- Use private inheritance in D
- It cannot be prevented
- Inherit A virtually: class B : virtual public A and class C : virtual public A
Show the answer
D. Inherit A virtually: class B : virtual public A and class C : virtual public A
With virtual inheritance B and C share one A subobject inside D, so calls to A's members are no longer ambiguous. The most-derived class, D, is then responsible for constructing A.
This topic is named in a candidate’s interview report: Software engineer on-campus interview, 2023, on GeeksforGeeks.
-
How do C++ compilers usually make a virtual function call through a base-class pointer reach the right override?
- They inline every virtual call
- Each object of a class with virtual functions holds a pointer to its class's vtable, a table of function pointers read at run time
- They look the function up by name at run time
- They generate a template for every derived class
Show the answer
B. Each object of a class with virtual functions holds a pointer to its class's vtable, a table of function pointers read at run time
A call such as p->area() loads the vtable pointer from the object, then the slot for area, then calls it. That indirection is why virtual calls are slightly slower and usually cannot be inlined.
This topic is named in a candidate’s interview report: Software engineer on-campus interview, 2023, on GeeksforGeeks.