Last active
July 26, 2024 19:13
-
-
Save M0nteCarl0/08e4e3cc6117ff470556eff0515c7e97 to your computer and use it in GitHub Desktop.
Senior Interview Q&A and code examples
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <concepts> | |
template<typename T> | |
concept Integral = std::is_integral<T>::value; | |
template<typename T> | |
void printValue(T value) requires Integral<T> { | |
std::cout << "Integral value: " << value << std::endl; | |
} | |
template<typename T> | |
void printValue(T value) { | |
std::cout << "Non-integral value: " << value << std::endl; | |
} | |
int main() { | |
printValue(10); // Calls the first overload (integral value) | |
printValue(3.14); // Calls the second overload (non-integral value) | |
printValue("Hello"); // Calls the second overload (non-integral value) | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
// Base class using CRTP | |
template <typename Derived> | |
class Base { | |
public: | |
void doSomething() { | |
static_cast<Derived*>(this)->implementation(); | |
} | |
// CRTP requires a derived class to define its own implementation | |
void implementation() { | |
std::cout << "Base implementation" << std::endl; | |
} | |
}; | |
// Derived class inheriting from the base class using CRTP | |
class Derived : public Base<Derived> { | |
public: | |
// Override the base class's implementation | |
void implementation() { | |
std::cout << "Derived implementation" << std::endl; | |
} | |
}; | |
int main() { | |
Derived d; | |
d.doSomething(); // Calls the derived class's implementation | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
#include <exception> | |
void threadFunction(std::exception_ptr& exPtr) { | |
try { | |
// Code that may throw an exception | |
throw std::runtime_error("Exception from thread!"); | |
} catch (...) { | |
// Capture the exception | |
exPtr = std::current_exception(); | |
} | |
} | |
int main() { | |
std::exception_ptr exPtr; | |
std::thread t(threadFunction, std::ref(exPtr)); | |
// Wait for the thread to finish | |
t.join(); | |
// Check if an exception was thrown in the thread | |
if (exPtr) { | |
try { | |
// Rethrow the exception | |
std::rethrow_exception(exPtr); | |
} catch (const std::exception& e) { | |
// Handle the exception in the main function | |
std::cout << "Exception caught in main: " << e.what() << std::endl; | |
} | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <future> | |
#include <exception> | |
void threadFunction() { | |
// Code that may throw an exception | |
throw std::runtime_error("Exception from thread!"); | |
} | |
int main() { | |
std::future<void> future = std::async(std::launch::async, threadFunction); | |
try { | |
// Wait for the thread to finish and propagate any exceptions | |
future.get(); | |
} catch (const std::exception& e) { | |
// Handle the exception in the main function | |
std::cout << "Exception caught in main: " << e.what() << std::endl; | |
} | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <thread> | |
#include <future> | |
void divideByZero(std::promise<int>& p) { | |
try { | |
int result = 10 / 0; | |
p.set_value(result); | |
} catch (...) { | |
p.set_exception(std::current_exception()); | |
} | |
} | |
int main() { | |
std::promise<int> p; | |
std::future<int> f = p.get_future(); | |
std::thread t(divideByZero, std::ref(p)); | |
try { | |
int result = f.get(); | |
std::cout << "Result: " << result << std::endl; | |
} catch (const std::exception& e) { | |
std::cout << "Exception caught: " << e.what() << std::endl; | |
} | |
t.join(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <functional> | |
void performOperation(int a, int b, std::function<int(int, int)> operation) { | |
int result = operation(a, b); | |
std::cout << "Result: " << result << std::endl; | |
} | |
int main() { | |
// Lambda expression as a function argument | |
performOperation(5, 3, [](int x, int y) { | |
return x + y; | |
}); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyClass.cpp - Private implementation | |
#include "MyClass.h" | |
// Private implementation class | |
class MyClass::Impl { | |
public: | |
void doSomething() { | |
// Implementation details | |
} | |
}; | |
MyClass::MyClass() : pImpl(new Impl()) {} | |
MyClass::~MyClass() { | |
delete pImpl; // Clean up the implementation object | |
} | |
void MyClass::doSomething() { | |
pImpl->doSomething(); // Delegate the call to the implementation object | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// MyClass.h - Public interface | |
class MyClass { | |
public: | |
MyClass(); | |
~MyClass(); | |
void doSomething(); | |
private: | |
class Impl; // Forward declaration of the private implementation class | |
Impl* pImpl; // Pointer to the implementation class | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <memory> | |
#include <iostream> | |
class Resource { | |
public: | |
Resource() { | |
std::cout << "Resource acquired" << std::endl; | |
} | |
~Resource() { | |
std::cout << "Resource released" << std::endl; | |
} | |
void DoSomething() { | |
std::cout << "Doing something with the resource" << std::endl; | |
} | |
}; | |
void UseResource() { | |
auto ptr = std::make_unique<Resource>(); | |
// Use the resource | |
ptr->DoSomething(); | |
// The resource will be automatically released when the unique_ptr goes out of scope | |
} | |
int main() { | |
UseResource(); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <type_traits> | |
// Function template that uses SFINAE to enable/disable based on type traits | |
template <typename T> | |
typename std::enable_if<std::is_integral<T>::value, void>::type | |
printValue(T value) { | |
std::cout << "Integral value: " << value << std::endl; | |
} | |
template <typename T> | |
typename std::enable_if<std::is_floating_point<T>::value, void>::type | |
printValue(T value) { | |
std::cout << "Floating-point value: " << value << std::endl; | |
} | |
int main() { | |
printValue(10); // Calls the first specialization (integral value) | |
printValue(3.14); // Calls the second specialization (floating-point value) | |
printValue("Hello"); // Compilation error, no matching specialization | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment