Observer to review my C Interview response

BobTPH

Joined Jun 5, 2013
11,537
Well, now it sounds like you paraphrased it from a Wikipedia entry instead if just memorized it word for word. I guess that is an improvement, but I don't think you get the gist of what I was trying to say. Do you converse with your colleagues that way?

What if you had a friend who was trying to learn C and asked you what storage classes were. Is that how you would answer?

Just talk to the guy/gal like you would to a normal person.

I would probably answer that question with a question of my own:

“Do you mean storage classes as a concept, or the details of how you specify them with C defaults and keywords.”

Don’t be afraid to clarify or narrow a question, it actually makes you look more knowledgeable than just spouting out everything you can think of on the topic. Especially if your answer would involve multiple paragraphs as above.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
interviewer gave me the following code and asked where the program starts execution:

C:
#include <stdio.h>

int count = 10; // Global variable

int main() {
    printf("Initial count: %d\n", count);
    count++;
    printf("Updated count: %d\n", count);
    return 0;
}
I answered that it starts from main() function (line 5). The interviewer then asked how the program knows the value of the global variable count if execution starts at main(), suggesting that the program might throw an error.

I responded by saying, "I don't know exactly where global variable initialization happens, but it should occur before main() is called." I believe that global variables are initialized before the program enters main(),

I would appreciate any feedback on how I could better explain this process.
 

BobTPH

Joined Jun 5, 2013
11,537
Global variables with static initializations are generally simply read from the executable file just as the program code is.

I think what the interviewer as getting at, however is that there is initialization code run before main is executed, so you actually gave an acceptable answer.
 
Last edited:

Beau Schwabe

Joined Nov 7, 2019
186
Dang!! .... I lost out on being hired once because I didn't know the maximum number of nodes you could have on CANBUS. This was for a trucking company and I was quite literally thrown right under the truck over a Zoom Call. The answer BTW is (32, 64, or 127) depending on what chips are used, but generally 64 is considered the limit.

Since then I have developed my own bespoke communication that is similar to CANBUS, only it is based on an industrial 24V system that can have over 1000 uniquely addressable nodes.

Fast forward a few years and I reached back out to that company, not to be hired, but only to offer our solution to them.


The point of this RANT? Always be optimistic and sure of yourself and keep your network channels open for opportunity.
If you don't know the answer to something don't be afraid to admit that you don't know, but do find out the answer. Never try to make up a long winded answer.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I was given char *str = "Hello, World!"; and asked to explain it.

My Response: I explained that this is a string literal, and str is a pointer pointing to the memory location where the literal is stored.

Question: Can we modify this string at runtime?
My Response: No, because it’s stored in read-only memory, and attempting to modify it would likely result in an error.

Question: Where would const int var = 1; be stored in memory?
My Response: I said it would also likely be in read-only memory, although I noted that storage specifics can vary depending on the compiler.

Question: Can a const variable be modified?
My Response: It cannot be modified directly, but it can be changed indirectly using a pointer.

Question: If both const variables and string literals are stored in read-only memory, why can we indirectly modify a const variable but not a string literal?
My Response: sorry but I don't know exact reason

I’d appreciate any feedback on my responses and intrested to understand reason of last question which I don't understand
 

BobTPH

Joined Jun 5, 2013
11,537
Your answer about modifying the const variable is incorrect. It would be an incorrect (non-conforming) program if your code attempts to do so. The result is implementation dependent. It might do what you expect, or cause a compilation error, or a runtime exception, or it might even modify the wrong memory, depending on the implementation.

Or, to look at it another way. You should have answered no, because the pointer would have to be declared as:

const char *var

and the compiler would not allow you use *var as an l-value (i.e. assign to it). But there are ways to get around that via a cast.

In the second example, an initialized int, a good compiler might treat it the same as:


#define var 1

In other words, it would not be assigned to memory at all.

The interviewer did a good job here, probing the limits of your knowledge.

After you write and debug about a hundred thousand lines of C, you might want to try to interview there again. They were clearly looking for a more experienced person than you.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Your answer about modifying the const variable is incorrect. It would be an incorrect (non-conforming) program if your code attempts to do so. The result is implementation dependent. It might do what you expect, or cause a compilation error, or a runtime exception, or it might even modify the wrong memory, depending on the implementation.
Thank you for the feedback. There may have been some confusion, as the interviewers question was more about comparing two statements: modifying a const variable indirectly versus modifying a string literal indirectly in read-only memory. why we can indirectly modify a const variable but not a string literal indirectly ?

Edit : posted code for your reference
C:
#include <stdio.h>

int main() {
    const int var = 10;      // const variable
    int *ptr = (int *)&var;

    printf("Original value of var: %d\n", var);

    *ptr = 20;  // modifying the value of var through the pointer

    printf("Modified value of var: %d\n", var);

    return 0;
}
 
Last edited:

BobTPH

Joined Jun 5, 2013
11,537
If you are saying the interviewer was confused, or I was confused, you are wrong. As I said, the interviewer did a good job.

I think you are confusing three issues.

1. Can you write C code that attempts to modify a const variable or string literal? Answer: yes to both.

example:

const char *con = “hello”;
// *con = ‘x’; — should result in a compile
// time error
char *var = (char *)con;
*var = ‘x’;

2. Would it work as expected? i.e. would var (and con) now point to the data “xello”? Answer: depends on the implementation.

3. What kind if memory is used for const objects? Answer: depends on the implementation.

Do you realize that not all systems have read only memory?
 

BobTPH

Joined Jun 5, 2013
11,537
Just to make you feel better, I did not get an offer after my first interview with Microsoft.

I think this is the question I botched:

After all my technical interviews with several groups, the HR person asked me:

“Which of the jobs I was interviewed for would I be interested in?”

I answered:

“None of them.”

Or it might have been this answer:

“If you come to work here, you will have to work really hard, are you ready for that?”

My answer:

<chuckle> “If I come to work here I will feel like I am on vacation.”

I guess that came across a a but cocky, but judge for yourself:

I had just spent a year developing, alone, a complete Modula-2 programming system for PC, including compiler, linker, text editor, debugger, make utility, library utility, and IDE, and released a product that got great reviews, but not enough sales to make me a living. I subsequently was bought out by another company who was one of my users.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
They were clearly looking for a more experienced person than you.
I am a fresher candidate, and interview was a valuable learning opportunity for me. I appreciate the challenging questions, as they encouraged me to think critically about these concepts.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I think you are confusing three issues.

1. Can you write C code that attempts to modify a const variable or string literal? Answer: yes to both.

2. Would it work as expected? i.e. would var (and con) now point to the data “xello”? Answer: depends on the implementation.

3. What kind if memory is used for const objects? Answer: depends on the implementation.
Thank you @BobTPH again for clarifying how these behaviors can vary by implementation, which helps me understand the limitations across different environments.

When I mentioned that a const variable could be modified indirectly using a pointer, it was based on my experience testing similar code. On GCC with Windows, I was able to modify const variables indirectly using pointers. However, for string literals, I had read somewhere that modifying them indirectly using a pointer isn’t possible.

Now I attempted this with GCC and windows , I didn’t receive any errors, but I also didn’t get any output

C:
#include <stdio.h>

int main() {
    const char *con = "hello";
    char *var = (char *)con;

    // Attempt to modify the string literal
    *var = 'x';

    printf("con: %s\n", con);
    printf("var: %s\n", var);

    return 0;
}
 
Last edited:

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Question: What's the difference between extern global and extern local variables?

My Answer:
Declaring extern outside a function provides file access, meaning it can be accessed by any function within the same file. However, declaring extern inside a function gives it function-only access, so it’s only accessible within that specific function where it’s declared.

Could you @BobTPH confirm if answer is correct,
 
Last edited:

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Nice looong thread. Longer than the interview, it seems.
This isn’t just about a single interview questions. I’m discussing questions from multiple interviews where I feel unsure if my responses were correct or could be improved. My goal is to understand where I might be going wrong in my answers.

An observer is important because they can help me identify areas for improvement in my answers, ensuring I'm presenting my knowledge effectively
 

BobTPH

Joined Jun 5, 2013
11,537
Question: What's the difference between extern global and extern local variables?

My Answer:
Declaring extern outside a function provides file access, meaning it can be accessed by any function within the same file. However, declaring extern inside a function gives it function-only access, so it’s only accessible within that specific function where it’s declared.

Could you @BobTPH confirm if answer is correct,
It is correct. But I would have answered simply: “The scope of the name.”
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
Interviewer: Can you explain why we would use dynamic memory

Answer: Dynamic memory is useful when we don’t know the exact amount of memory required at compile time

Interviewer: But look at this code, the program doesn't know the size of n at compile time but still allocates, store the values. Why would we need dynamic memory if we can achieve this with static memory?

C:
#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of integers you want to store: ");
    scanf("%d", &n);
    int arr[n]; 
    // Input values into the array
    printf("Enter %d integers:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Display stored values
    printf("Stored values: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
Answer : I'm sorry, I don’t know the exact answer you're looking for, but here’s what I understand, dynamic memory's ability to release memory with free vs. static memory's fixed allocation.

Is that correct, or is there anything you’d like me to clarify further?
 

WBahn

Joined Mar 31, 2012
32,890
Interviewer: Can you explain why we would use dynamic memory

Answer: Dynamic memory is useful when we don’t know the exact amount of memory required at compile time

Interviewer: But look at this code, the program doesn't know the size of n at compile time but still allocates, store the values. Why would we need dynamic memory if we can achieve this with static memory?

C:
#include <stdio.h>

int main() {
    int n;
    printf("Enter the number of integers you want to store: ");
    scanf("%d", &n);
    int arr[n];
    // Input values into the array
    printf("Enter %d integers:\n", n);
    for (int i = 0; i < n; i++) {
        scanf("%d", &arr[i]);
    }

    // Display stored values
    printf("Stored values: ");
    for (int i = 0; i < n; i++) {
        printf("%d ", arr[i]);
    }
    printf("\n");

    return 0;
}
Answer : I'm sorry, I don’t know the exact answer you're looking for, but here’s what I understand, dynamic memory's ability to release memory with free vs. static memory's fixed allocation.

Is that correct, or is there anything you’d like me to clarify further?
I wouldn't be too impressed by this answer. First, local variables in functions are automatic, so they are deallocated when the function returns.

Your first answer is correct, at least in broad brush strokes, but smacks of being a memorized and regurgitated response to a stock question without demonstrating any actual understanding of what it means. Hence, the follow up question to divine if that is the case, and your follow-up answer pretty well confirms that it is.

You said that dynamic memory is used when you don't know how much memory is needed at compile time. Well, that was true at one point, but C99 added variable-length arrays which relaxed that restriction allowing runtime allocation of arrays provided the size of the array was known at the time that the array was defined, which in this example it is. You should have been able to take your prior answer and ask yourself how it might still apply to this situation. Instead, you basically threw up your hands and resorted to regurgitating an essentially unrelated memorized tidbit.

But the basic argument still applies. What if you don't know how big the array needs to be at the time you start using it. For instance, you are reading data from some source and need to store the data as it is read, but won't know that the end of the data has been reached until some sentinel value is seen or some other event tells you that there is no more data. One example might be reading lines of text from a file in which you want to have the entire contents of the file in memory as individual lines but won't know how many there are until you actually read them all in and reach the end of the file. You also won't know how long a given line is going to be until you reach the end of that line.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
I wouldn't be too impressed by this answer.
Thank you for your feedback. While I have never worked with files, I believe that linked lists serve as a suitable example of dynamic memory allocation when the size is unknown. This approach allows for easy addition and removal of nodes and ensures efficient memory usage, as it is allocated only when needed.

Could you share what you consider to be a relevant answer for last question of interviewer
 
Last edited:

BobTPH

Joined Jun 5, 2013
11,537
You continue to come across as someone who has memorized a textbook and has never written a program. This impression is what need to dispel.
 

Thread Starter

Kittu20

Joined Oct 12, 2022
511
You continue to come across as someone who has memorized a textbook and has never written a program. This impression is what need to dispel.
I'm not sure what the best answer to the interviewer's last question should be.

Interviewer: But look at this code, the program doesn't know the size of n at compile time but still allocates, store the values. Why would we need dynamic memory if we can achieve this with static memory?
I presented two answers, but you both mentioned they may not be fully relevant.

Could you please share what you think a relevant answer would be? Apologies for asking so directly.
 
Top