Count C++ Constructor and Assignment Calls
Updated
Problem statement
Count constructor and assignment calls in the C++ program below.
For this exercise, assume each declared special member increments only its own observable counter. It does not call another special member, and every object stays usable after a move. The two assignments return *this by reference. Only calls on Item count. Destructors are not counted.
struct Item {
Item();
Item(const Item&);
Item(Item&&) noexcept;
Item& operator=(const Item&);
Item& operator=(Item&&) noexcept;
};
void passValue(Item value) {}
void bindConstRef(const Item& value) {}
void bindRvalueRef(Item&& value) {}
// Reset the five counters to zero before this code.
Item a, b;
for (const string& operation : operations) {
if (operation == "PASS_LVALUE") passValue(a);
else if (operation == "PASS_MOVE") passValue(std::move(a));
else if (operation == "BIND_CONST_REF") bindConstRef(a);
else if (operation == "BIND_RVALUE_REF") bindRvalueRef(std::move(a));
else if (operation == "COPY_ASSIGN") b = a;
else if (operation == "MOVE_ASSIGN") b = std::move(a);
}Return exactly five integers in this order: ordinary default construction, copy construction, move construction, copy assignment, and move assignment. Include the two constructions before the loop. Reference binding and std::move by themselves create no object. The fixture uses named existing objects and no return-by-value or optional copy-elision case.
Function
countCppCalls(operations: String[]) → int[]Examples
Example 1
operations = []return = [2, 0, 0, 0, 0]The two named objects a and b are default-constructed before the loop. There are no other calls.
Example 2
operations = ["PASS_LVALUE", "PASS_MOVE", "BIND_CONST_REF", "BIND_RVALUE_REF"]return = [2, 1, 1, 0, 0]Passing a as a value copies it once. Passing std::move(a) as a value moves once. Both reference-binding calls create no Item, so no assignment runs.
Example 3
operations = ["COPY_ASSIGN", "MOVE_ASSIGN", "PASS_LVALUE", "COPY_ASSIGN"]return = [2, 1, 0, 2, 1]The assignments invoke copy assignment twice and move assignment once. The by-value lvalue call adds one copy construction; the two initial default constructions still count.
Constraints
- 0 <= operations.length <= 10000
- Each operation is exactly one of
PASS_LVALUE,PASS_MOVE,BIND_CONST_REF,BIND_RVALUE_REF,COPY_ASSIGN, orMOVE_ASSIGN.
More Squarepoint Capital interview questions
- Mutable Record Store with Error ResultsONSITE INTERVIEW · Seen Sep 2026
- Maximum SubarrayONSITE INTERVIEW · Seen Aug 2026
- Product of Array Except SelfONSITE INTERVIEW · Seen Aug 2026
- Estimate Pi with a Finite SeriesONSITE INTERVIEW · Seen Oct 2025
- In-Memory Directory and File OperationsONSITE INTERVIEW · Seen Oct 2025
- Observe a Lazy GeneratorONSITE INTERVIEW · Seen Feb 2025
- Order Matching on ExecuteONSITE INTERVIEW · Seen Dec 2024
- Accumulate Numeric Value ObjectsONSITE INTERVIEW · Seen Sep 2024
- Differentiate a PolynomialONSITE INTERVIEW · Seen Aug 2023
- Newton Square Root to a Guaranteed TolerancePHONE SCREEN · Seen Nov 2026
Similar questions at other companies
- Ring Buffer OperationsAmazon · ONSITE INTERVIEW
- Count Distinct Underwriting PII ValuesAffirm · PHONE SCREEN · ONSITE INTERVIEWAffirm interview questions
- Bounded Word Guess SolverAtlassian · ONSITE INTERVIEWAtlassian interview questions
- Generate an Unbiased Bit from a Biased CoinGoogle · ONSITE INTERVIEWGoogle interview questions
- Insert After a Linked-List NodeAMD · ONSITE INTERVIEWAMD interview questions
- Document Edit EventsClickUp · ONSITE INTERVIEW
- Virtual Card Transaction ValidatorCapital One · ONSITE INTERVIEWCapital One interview questions
- Apply Overridden Integer TransformsOptimus Information · ONSITE INTERVIEWOptimus Information interview questions
- Basic String Compression with Length GuardNew Relic · PHONE SCREEN · ONSITE INTERVIEW
- Copy File Bytes with Partial WritesMicrosoft · ONSITE INTERVIEWMicrosoft interview questions