Samsung Interview Question for Software Engineer / Developers


Country: -
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

A C program should be faster than C++ program.
For Example, let us take an example of polymorphism. The C++ has dynamic or runtime polymorphism. So the function call on an object will result in v-table v-function etc etc which will certainly take time. However in C, we have static polymorphism or function overloading in which the flow control is decided at the compile time thus saving the additional steps at runtime.

C++ however makes possible to write better code, reusable but this comes at a little price.

- abhityagi85 October 18, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

C++ follows a pay-as-you-go philosophy. In other words, unless you opt to use the more advanced features of the language, the code isn't any slower than if you had written C. That is, you could write a C program that would also be a valid C++ program and the two would be equally fast.

- eugene.yarovoi October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I liked that "pay-as-you-go."
But it breaks my heart when you said 'ANY' ... C++ has to be slower than C, though negligibly for the reasons stated.

- abhityagi85 October 24, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

C++ doesn't have to be slower; that's my whole point. C++ is only slower when you use certain C++ features. Stick to only the C features and it'll be just as fast.

C++ does not have dynamic polymorphism when you don't use it. Generally, functions are, in fact, bound at compile time -- only functions marked virtual have dynamic polymorphism. Otherwise, C++ wouldn't be a pay-as-you-go language.

Yes, dynamic polymorphism sometimes allows us to write more reuseable code at the cost of some performance, but that is a tradeoff that you choose to make on a per-function basis in C++. You ask yourself whether you need the behavior for each function. You could decide to not use it at all, if the problem you're trying to solve doesn't need it.

- eugene.yarovoi October 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Pay-A-You-Go What does it means?
Do you want to say I will not use c++ features so my code will run equally fast. If you dont use c++ features, what do you compare your C code with. There is no point of comparison of C and C++ till you use the c++ features.
There is no point of comparing c and c++ on basic of compiling same code in '.c' and '.cpp' files as mentioned by you as 'functions'.
If you '.c' and '.cpp' extension is not responsible for performance, It is senseless and illogical!!

- abhityagi85 October 25, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can take C code and call it C++, and then there won't be a performance hit. So C++ can be as fast as C, if it's C-compatible C++ code.

If you use C++-specific features, your code may be slower, but that depends. If you used those features because you really needed them and you end up doing some crazy workaround in the C version, you may essentially be writing an ad-hoc, poorly-documented, bug-ridden, slow implementation of C++. Then C++ might be faster because the C++ compiler may have better optimizations than your ad-hoc implementation.

Another important point is that there are plenty of C++-only features that are not necessarily any slower than their C counterparts. For example, templates allow a great deal of code re-use without any performance drawbacks (since a different version will be made for each type at compile time). As another example, operator overloading is no slower than using functions to achieve the same effects in C -- internally, the operators are converted to function calls anyway.

- eugene.yarovoi November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

compilation and linking takes time in C++ but both do same at execution

- lakshmana May 05, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

eugene, I was happy to see pay-as-you-go used by you. Thats the whole point started from your post and now you say equally fast. C++ is based on a design pattern of Object Orient ism. Now thats the whole point of getting slower code. If you say just take a .c file and rename it to .cpp and it will run equally fast. Your ideology of c++ itself is wrong then. C++ means object oriented approach and reusability. as soon as reusability comes into picture, virtual usually start coming with it and ultimately little bit slower code.
You cant just write a "HelloWorld" program and say that -
hey both C and C++ are equally fast. I am not going to reply any more posts as its simple and not worth explaining.

- abhityagi85 May 07, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@abhityagi85: I don't think we disagree on any technical subject matter here. If we disagree, it's only on whether a C program that passes C++ syntax checks (and many C programs do) can properly be considered a C++ program. I say yes because it complies with the syntax rules of the C++ language. Even if you think that such a program would constitute bad C++ style, a bad C++ program is still a C++ program. So for such a program, it could be as fast in C++ as in C.

You're saying that C++ is about object-orientation, and so in practice, C++ programs will leverage this and have the potential to be slower. I don't disagree with that.

- eugene.yarovoi May 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

go to hell everyone

- mmmm November 14, 2018 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Well, since most valid C programs are valid C++ programs, I think this question is intentionally stupid. I think the interviewer wants to see how you handle being asked a stupid question.

- Anonymous October 09, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

To keep the argument going.. if you write a C program in C++ compiler, its again a C program ...

So we just need to talk about a C++ program and a C program. There is no doubt C will be faster as C++ use dynamic polymorphism which will slow..

- vijaymukilan July 16, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The use of rereference arguments, allowed in C++ and not so in C (as far as I know) can mean that a C++ program can be faster than C.

- Henri de Feraudy November 19, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Pointers/Functions/OOP that's all I have to say about C++

- Meh May 31, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I agree with eugene.yarovoi ,his very rational point of view . (Of course ,after 8 years though xD )

- Anonymous December 18, 2019 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More