Coding test
Here's a simple C++ example demonstrating a basic class:
#include <iostream>
#include <string>
class Person {
private:
std::string name;
int age;
public:
Person(const std::string& n, int a) : name(n), age(a) {}
void display() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
void setAge(int newAge) {
age = newAge;
}
};
int main() {
Person person("John Doe", 25);
person.display();
person.setAge(26);
person.display();
return 0;
}
This code demonstrates:
- Class definition with private and public members
- Constructor initialization
- Const member function
- Basic input/output operations
- String handling
Here's a Python example showing list comprehension and dictionary handling:
def process_data(numbers):
# List comprehension
squares = [x**2 for x in numbers]
# Dictionary comprehension
counts = {x: numbers.count(x) for x in set(numbers)}
return squares, counts
# Example usage
data = [1, 2, 3, 2, 4, 1]
squared, frequency = process_data(data)
print(f"Squared numbers: {squared}")
print(f"Number frequencies: {frequency}")
Here's a JavaScript example demonstrating async/await and modern ES6+ features:
class DataService {
constructor(baseUrl) {
this.baseUrl = baseUrl;
}
async fetchData(endpoint) {
try {
const response = await fetch(`${this.baseUrl}/${endpoint}`);
if (!response.ok) throw new Error('Network response was not ok');
return await response.json();
} catch (error) {
console.error('Error fetching data:', error);
throw error;
}
}
}
// Example usage with arrow functions
const service = new DataService('https://api.example.com');
const getData = async () => {
const data = await service.fetchData('users');
return data.map(user => ({
...user,
fullName: `${user.firstName} ${user.lastName}`
}));
};
And here's a Java example showing generics and lambda expressions:
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
public class DataProcessor<T extends Number> {
private List<T> data;
public DataProcessor(List<T> data) {
this.data = data;
}
public List<T> filterGreaterThan(T threshold) {
return data.stream()
.filter(item -> item.doubleValue() > threshold.doubleValue())
.collect(Collectors.toList());
}
public double calculateAverage() {
return data.stream()
.mapToDouble(Number::doubleValue)
.average()
.orElse(0.0);
}
}
// Example usage
public class Main {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5);
DataProcessor<Integer> processor = new DataProcessor<>(numbers);
System.out.println("Average: " + processor.calculateAverage());
System.out.println("Numbers > 3: " + processor.filterGreaterThan(3));
}
}
These examples demonstrate various programming concepts and features across different languages:
- Python: List/dictionary comprehensions, function returns
- JavaScript: Classes, async/await, modern ES6+ syntax
- Java: Generics, lambda expressions, Stream API
Let's look at some diagrams using Mermaid:
Here's a flowchart showing a simple decision process:
Here's a sequence diagram showing a typical API interaction:
And here's a class diagram showing a simple inheritance structure:
These diagrams demonstrate different types of visualizations that can be created using Mermaid:
- Flowchart: Shows decision flow and process steps
- Sequence Diagram: Illustrates interactions between components over time
- Class Diagram: Represents object-oriented relationships and structure