Hot Topics
Code Nation Solution
Technical Round
- Question 1
You’re given a string that (may) be appended with a number at last. You need to find whether the length of string excluding that number is equal to that number.
- Answer
You can check if a given string ends with a number and extract the number, then compare its length to the length of the string without the number. Here’s an example implementation in Python
def check_string(s):
try:
num = int(s[-1])
string_without_num = s[:-1]
return len(string_without_num) == num
except ValueError:
try:
num = int(s[-2:])
string_without_num = s[:-2]
return len(string_without_num) == num
except ValueError:
return False
This function uses the try
…except
statement to handle cases where the last character or the last two characters of the string are not a number. In such cases, the ValueError
exception is raised and the function returns False
.
If the last character or the last two characters are a number, the function converts the number to an integer using int
and extracts the string without the number. The function then returns True
if the length of the string without the number is equal to the number, and False
otherwise.
- Question 2
How polymorphism/runtime binding is actually implemented?
- Answer
Polymorphism and runtime binding are implemented in object-oriented programming languages such as Java, C++, and Python through a mechanism called dynamic dispatch.
Dynamic dispatch is the process of selecting which implementation of a polymorphic method to call at runtime based on the type of the object for which it is called. This allows for the same method to have different implementations for different classes.
In most object-oriented programming languages, polymorphism and runtime binding are implemented through a mechanism called virtual function tables (VTables). VTables are data structures that associate method names with the addresses of their implementations for each class.
When a polymorphic method is called on an object, the runtime system looks up the correct implementation of the method in the object’s class’ VTable and calls that implementation. This allows the correct implementation of the method to be selected at runtime based on the type of the object.
The implementation details of dynamic dispatch and VTables can vary between programming languages and the underlying computer architecture, but the basic mechanism remains the same. The use of dynamic dispatch and VTables allows for polymorphism and runtime binding, which are key features of object-oriented programming and play a crucial role in enabling code reuse, abstraction, and encapsulation.
- Question 3
Asked basic question on how to multiply a number if the processor did not
- Answer
If a processor does not have a built-in instruction to multiply two numbers, you can still perform multiplication using addition. You can add one number to itself a certain number of times to obtain the product of the two numbers. For example, to multiply 3 by 4, you can add 3 to itself 4 times: 3 + 3 + 3 + 3 = 12.
- Question 4
Difference between list and set.
- Answer
List | Set |
1. List is indexed sequence. | Set is non indexed sequence. |
2. List can store duplicate elements. | Set cannot store duplicate elements. |
3. Elements can be accessed by their position. | Elements cannot be accessed by their position. |
4. We can store multiple null values in list | Only one null value is stored in set. |