Coding test

Published on 2025-03-27 by Mohd

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:

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:

Let's look at some diagrams using Mermaid:

Here's a flowchart showing a simple decision process:

%%{init: {'theme': 'dark'}}%%
flowchart TD
    A([Start]) --> B{Is it working?}
    B -- Yes --> C([Great!])
    B -- No --> D([Debug])
    D --> E{Found issue?}
    E -- Yes --> F([Fix it])
    E -- No --> G([Ask for help])
    F --> B
    G --> B
    C --> H([End])

style A fill:#1e40af,stroke:#fff
style B fill:#1e40af,stroke:#fff
style C fill:#1e40af,stroke:#fff
style D fill:#1e40af,stroke:#fff
style E fill:#1e40af,stroke:#fff
style F fill:#1e40af,stroke:#fff
style G fill:#1e40af,stroke:#fff
style H fill:#1e40af,stroke:#fff

Here's a sequence diagram showing a typical API interaction:

sequenceDiagram
    rect rgb(255, 255, 255)
    participant Client
    participant API
    participant Database

    Client->>API: POST /api/data
    API->>Database: Validate & Store
    Database-->>API: Success
    API-->>Client: 200 OK

    Client->>API: GET /api/data
    API->>Database: Query Data
    Database-->>API: Return Results
    API-->>Client: 200 OK with Data
    end

And here's a class diagram showing a simple inheritance structure:

%%{init: {'theme': 'dark'}}%%
classDiagram
    class Animal:::darkblue {
        +String name
        +int age
        +makeSound()
        +move()
        +eat()
    }
    
    class Dog:::darkblue {
        +String breed
        +bark()
        +fetch()
    }
    
    class Cat:::darkblue {
        +boolean isIndoor
        +meow()
        +climb()
    }
    
    Animal <|-- Dog
    Animal <|-- Cat

    classDef darkblue fill:#1e40af,stroke:#fff,color:#fff

These diagrams demonstrate different types of visualizations that can be created using Mermaid: