Autocoder

Siemens Coding Round Questions with Solutions

Last updated 2026-11-19

Technical tests for graduate trainee roles at a company like Siemens are reported to cover C and C++, Linux and databases, so the practice problems here mix C output prediction, small string and array functions, a SQL query and a linked-list problem. Each of the twelve has a complete, runnable solution, its complexity and the usual mistake.

They are practice problems for topics the tests are commonly reported to cover, not real papers. Pair this page with Siemens interview questions and how to crack the technical MCQ test.

How to approach them

For output prediction, write the type of each expression and note whether the evaluation order is defined. Printing with nested calls or several side effects in one statement is a favourite trap, and the right answer is sometimes that the order is unspecified.

For function problems, say what happens for an empty input and for one element before writing code.

Hiring status

No sourced hiring facts for Siemens are held on this site, so no dates, openings or packages are quoted. Use the company's own careers site, jobs.siemens.com.

Siemens Coding Round Questions with Solutions: questions with worked answers

  1. 1

    Predict the output: how many times is x printed after two fork() calls?

    medium

    Each fork doubles the number of processes, so after two calls there are four, and each of them executes the printf. The program prints x four times, on one line if there is no newline, though the process that finishes last may print after the shell prompt. Time and space are constant per process. Common mistake: answering 3 or 2, forgetting that the child also continues to run the second fork.

    c
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/wait.h>
    int main(void) {
        fork();
        fork();
        printf("x");
        fflush(stdout);
        while (wait(NULL) > 0) { }
        return 0;
    }
  2. 2

    Predict the output of printf("%d", printf("ab")).

    medium

    The inner printf runs first, printing ab and returning the number of characters written, 2. The outer printf then prints that 2. Output: ab2. Common mistake: expecting 2ab, or forgetting that printf returns a value. The same idea explains why the return value of scanf is checked.

    c
    #include <stdio.h>
    int main(void) {
        printf("%d", printf("ab"));
        printf("\n");
        return 0;
    }
  3. 3

    Predict the output: assignment inside an if condition.

    easy

    if (x = 5) assigns 5 to x and tests the value of the assignment, which is non-zero, so the branch runs and prints 5 on the following line. The compiler usually warns about this. Output: x is 5. Write 5 == x if you want the comparison, or enable warnings with -Wall. Common mistake: expecting the comparison and the else branch.

    c
    #include <stdio.h>
    int main(void) {
        int x = 10;
        if (x = 5) printf("x is %d\n", x);
        else printf("else\n");
        return 0;
    }
  4. 4

    Predict the output: sizeof of an array versus strlen of the string it holds.

    medium

    s is a char array of 10 bytes initialised with "hi", so sizeof(s) is 10 while strlen(s) is 2, the number of characters before the terminator. The remaining bytes are zero. Output: 10 2. Common mistake: treating sizeof as the string length, or using strlen on a buffer that is not null-terminated, which reads beyond it.

    c
    #include <stdio.h>
    #include <string.h>
    int main(void) {
        char s[10] = "hi";
        printf("%zu %zu\n", sizeof(s), strlen(s));
        return 0;
    }
  5. 5

    Predict the output: the comma operator.

    medium

    The comma operator evaluates its left operand, sequences it, then evaluates and returns its right operand. In (i++, i + 5), i becomes 1 and the result is 1 + 5 = 6, which is assigned to i. Output: 6. This is well defined because there is a sequence point at the comma, unlike i = i++ + 1, which is undefined. Common mistake: confusing it with the commas separating function arguments, which are not operators.

    c
    #include <stdio.h>
    int main(void) {
        int i = 0;
        i = (i++, i + 5);
        printf("%d\n", i);
        return 0;
    }
  6. 6

    Reverse the words in a sentence.

    easy

    Split on whitespace, reverse the list and join with single spaces. For "embedded systems are fun" the result is "fun are systems embedded". Time O(n), space O(n). Common mistake: reversing the characters of the whole string instead of the word order, or leaving extra spaces when the input has multiple spaces; split() without an argument handles both.

    python
    def reverse_words(s):
        return " ".join(reversed(s.split()))
    
    print(reverse_words("embedded systems are fun"))  # fun are systems embedded
  7. 7

    Find the second-highest salary using SQL, then check it with an in-memory database.

    medium

    Take the maximum salary below the overall maximum, which handles duplicates of the top value. The query returns NULL if there is only one distinct salary. The Python harness below runs it on SQLite with the salaries 50, 80, 80, 60 and prints 60. Time is O(n) for the aggregate scans. Common mistake: using ORDER BY with OFFSET 1 without DISTINCT, which returns 80 again when two people share the top salary.

    python
    import sqlite3
    db = sqlite3.connect(":memory:")
    db.execute("CREATE TABLE emp(name TEXT, salary INT)")
    db.executemany("INSERT INTO emp VALUES (?, ?)", [("a", 50), ("b", 80), ("c", 80), ("d", 60)])
    q = "SELECT MAX(salary) FROM emp WHERE salary < (SELECT MAX(salary) FROM emp)"
    print(db.execute(q).fetchone()[0])  # 60
  8. 8

    Implement strcmp-like comparison of two C strings.

    easy

    Walk both strings while the characters are equal and not the terminator, then return the difference of the first differing characters, compared as unsigned char. Return 0 if both end together. Time O(n), space O(1). Common mistake: comparing as signed char, which orders characters above 127 incorrectly, and not stopping at the terminator.

    c
    #include <stdio.h>
    int my_strcmp(const char *a, const char *b) {
        while (*a && *a == *b) { a++; b++; }
        return (unsigned char)*a - (unsigned char)*b;
    }
    int main(void) {
        printf("%d %d %d\n", my_strcmp("abc", "abc") == 0, my_strcmp("abc", "abd") < 0, my_strcmp("b", "a") > 0);
        /* 1 1 1 */
        return 0;
    }
  9. 9

    Check whether two strings are anagrams.

    easy

    Count the characters in both and compare the counters; equal counts mean anagrams. "listen" and "silent" match; "rat" and "car" do not. Time O(n), space O(k) for k distinct characters. Sorting both strings also works in O(n log n). Common mistake: not checking lengths first, or ignoring case when the problem treats it as significant.

    python
    from collections import Counter
    
    def is_anagram(a, b):
        return len(a) == len(b) and Counter(a) == Counter(b)
    
    print(is_anagram("listen", "silent"), is_anagram("rat", "car"))  # True False
  10. 10

    Recursive binary search that returns the index or -1.

    medium

    Compare the middle element with the target and recurse on the half that can contain it. Use mid = lo + (hi - lo) / 2 to avoid overflow. In {2, 5, 8, 12, 16, 23} the value 16 is at index 4 and 7 is absent. Time O(log n), space O(log n) for the recursion (O(1) when written as a loop). Common mistake: infinite recursion from using mid instead of mid + 1 or mid - 1.

    c
    #include <stdio.h>
    int bs(const int *a, int lo, int hi, int t) {
        if (lo > hi) return -1;
        int mid = lo + (hi - lo) / 2;
        if (a[mid] == t) return mid;
        return a[mid] < t ? bs(a, mid + 1, hi, t) : bs(a, lo, mid - 1, t);
    }
    int main(void) {
        int a[] = {2, 5, 8, 12, 16, 23};
        printf("%d %d\n", bs(a, 0, 5, 16), bs(a, 0, 5, 7)); /* 4 -1 */
        return 0;
    }
  11. 11

    Find the longest common prefix of a list of strings.

    easy

    Take the first string as the candidate and shorten it until every other string starts with it. For ["flower", "flow", "flight"] the answer is "fl". Time O(total characters), space O(1) beyond the result. Common mistake: not handling an empty list or an empty string, which should return an empty prefix.

    python
    def lcp(words):
        if not words:
            return ""
        p = words[0]
        for w in words[1:]:
            while not w.startswith(p):
                p = p[:-1]
        return p
    
    print(repr(lcp(["flower", "flow", "flight"])), repr(lcp(["dog", "car"])))  # 'fl' ''
  12. 12

    Find the middle node of a singly linked list in one pass.

    medium

    Move a slow pointer one step and a fast pointer two steps; when fast reaches the end, slow is at the middle. For five nodes the middle is the third; for four nodes this returns the second of the two middle nodes. Time O(n), space O(1). Common mistake: counting the length first and traversing twice when one pass was asked.

    c
    #include <stdio.h>
    struct Node { int v; struct Node *next; };
    struct Node *middle(struct Node *h) {
        struct Node *s = h, *f = h;
        while (f && f->next) { s = s->next; f = f->next->next; }
        return s;
    }
    int main(void) {
        struct Node e = {5, NULL}, d = {4, &e}, c = {3, &d}, b = {2, &c}, a = {1, &b};
        printf("%d %d\n", middle(&a)->v, middle(&b)->v); /* 3 4 */
        return 0;
    }

Frequently asked questions

What kinds of problems are in a Siemens graduate technical test?

A third-party guide reports C and C++, Linux and database questions in the online technical test. Expect output prediction, short functions and SQL, at easy to medium level.

Do I need to run fork() examples?

Run them once to see the behaviour, but understand the reasoning: processes double with each call and output order is not defined.

Are these real Siemens problems?

No. They are practice problems in the style of topics the tests are commonly reported to cover. The company does not publish its papers.

How do I practise SQL for this test?

Use an in-memory SQLite database as in the example here, and write ten queries covering joins, aggregates, grouping and subqueries.

Sources

Related guides

More in Company Interview Questions

See all in this section