Templates and Generic Programming Interview Questions for C++
25 Templates and Generic Programming questions with answers for c++ roles — 6 easy, 13 medium, 6 hard.
About Templates and Generic Programming
Write code that works for any type using function and class templates. This topic covers specialization, variadic templates, fold expressions, type traits, SFINAE, C++20 concepts and compile-time computation with constexpr, along with practical concerns such as readable errors, code bloat and slow builds in template-heavy projects.
What are non-type template parameters?
Show answer
Write a generic function template that returns the larger of two values.
Show answer
Implement a generic Stack class template backed by std::vector.
Show answer
Compute a factorial at compile time.
Show answer
How do you keep your C++ knowledge up to date with new standards?
Show answer
Why are templates usually defined in header files?
Show answer
What is the difference between full and partial template specialization?
Show answer
What are C++20 concepts?
Show answer
When do you need the typename keyword inside a template?
Show answer
What are type traits and how does if constexpr use them?
Show answer
What is template argument deduction and class template argument deduction?
Show answer
A teammate complains that a template function produces hundreds of lines of compiler errors when misused. How do you improve this?
Show answer
A generic average function must accept only numeric types. How would you enforce that?
Show answer
You need a container that holds ints, strings and doubles together. Which approach would you choose?
Show answer
A generic maxOf template gives wrong results when called with two C-style string literals. Why, and how do you fix it?
Show answer
Write a variadic function that sums any number of arguments and one that prints them.
Show answer
Write a C++20 concept and use it to constrain a function.
Show answer
Tell me about a time you wrote generic, reusable code that saved effort for your team.
Show answer
What are variadic templates and fold expressions?
Show answer
What is SFINAE?
Show answer
The release binary has grown very large and analysis shows many template instantiations. What can you do?
Show answer
Build times have become very slow because heavy templates are included everywhere. How do you speed them up?
Show answer
How do you decide between templates and virtual functions for polymorphism?
Show answer
Implement your own is_pointer type trait.
Show answer
Frequently asked questions
How important is Templates and Generic Programming in C++ interviews?
Write code that works for any type using function and class templates. This topic covers specialization, variadic templates, fold expressions, type traits, SFINAE, C++20 concepts and compile-time computation with constexpr, along with practical concerns such as readable errors, code bloat and slow builds in template-heavy projects.
How should I use the Templates and Generic Programming sheet?
Go top to bottom — easy to hard. Answer before opening each answer, tick what you solved and save misses to Last-Minute Revision.