PPractick

DSA interview question

Functions: Pass by Value vs Pass by Reference

Easy · Algorithms · Topics: language basics

The problem

A teammate wrote a helper swapValues(a, b) to swap two scores in a sorting routine, but after calling it, nothing changes, and the sort never finishes correctly. Separately, a function that "adds a bonus point to every score" works on its own but the caller's list never updates. Both bugs have the same root cause: the functions received copies of the data. Your task: write the swap three ways (by value, by reference, and by pointer), show which ones actually change the caller's variables, and write a helper that returns two results (minimum and maximum) through reference parameters without copying the input.

Example: with a = 3, b = 7, swapByValue(a, b) leaves them as 3, 7, while swapByReference(a, b) makes them 7, 3.

Function signature

void swapByValue(int a, int b);                            // receives copies
void swapByReference(int& a, int& b);                      // receives aliases
void swapByPointer(int* a, int* b);                        // receives addresses
void minMax(const vector<int>& nums, int& lo, int& hi);   // read-only input, two outputs

Test cases

InputOutputExplanation
a = 3, b = 7, call swapByValue(a, b)a = 3, b = 7Only the local copies were swapped
a = 3, b = 7, call swapByReference(a, b)a = 7, b = 3References refer to the caller's variables
a = 3, b = 7, call swapByPointer(&a, &b)a = 7, b = 3Writes through the addresses
minMax({4, -2, 9}, lo, hi)lo = -2, hi = 9Two results returned through references
minMax({5}, lo, hi)lo = 5, hi = 5Single element
v = {1, 2}, addBonusByValue(v) then addBonusByReference(v)v = {2, 3}Only the by-reference call changes the caller's vector

(Confirm: nums is non-empty; the caller's variables are inspected after each call.)

How interviewers phrase it

Clarifying questions to ask

  1. Which language? (C++ supports all three; Java is always pass-by-value; Python passes object references.)
  2. Should the function modify the caller's data, or must the input stay unchanged?
  3. How large is the data being passed? (Large objects shouldn't be copied needlessly.)
  4. Is a null or missing argument possible? (Pointers can be null; references can't.)

Follow-ups to expect

  1. "Why does swapByValue not work?"
  2. "Reference vs pointer?"
  3. "Why pass a vector as const vector<int>&?"
  4. "Is Java pass-by-reference for objects?"
  5. "Can you return a reference to a local variable?"
  6. "How else can you return multiple values?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in