Summary
I had an interview at Arista Network focusing on C++ pointer concepts, including debugging code and fixing logical errors to achieve a desired output. I successfully navigated both parts of the problem.
Full Experience
My interview at Arista Network began with a C++ debugging problem. The interviewer presented me with a code snippet involving pointers and asked me to predict its output and explain the reasoning. I correctly determined that the output would be 'Value: 10' and explained that the function receives a copy of the pointer, so reassigning p within the function does not affect the original ptr in main. Following this, I was challenged to modify the function to make the program print 'Value: 100'. I proposed dereferencing p and assigning the value of x to the memory location it pointed to, thereby altering the value of a in main to 100.
Interview Questions (2)
What will be the output of the provided C++ code snippet and why?
#include <iostream> using namespace std;void function(int *p) { int x = 100; p = &x; }
int main() { int a = 10; int *ptr = &a; function(ptr); cout << "Value: " << *ptr << endl; return 0; }
Given the previous C++ code snippet, how would you modify the function to make it print Value: 100 instead of Value: 10, by changing only the function body?
Original function:
void function(int *p) {
int x = 100;
p = &x;
}