Samsung C Programming with DSA Interview Questions & Answers
Curated interview practice questions with answers. These are practice scenarios, not a verified list of past interview questions.
About Samsung hiring for C Programming with DSA roles
Samsung Electronics makes smartphones, consumer electronics, semiconductors and home appliances. Its India R&D institutes in Bengaluru, Noida and Delhi work on mobile software, AI, camera, connectivity and data. It hires for software engineers, test engineers and data professionals, with campus hiring known for coding-focused tests.
For C Programming with DSA roles, this Samsung practice set focuses most on Trees and Graphs (5), Pointers and Memory Management (4), Structures, Unions and File Handling (4), Linked Lists (4). It has 1 easy, 24 medium, 5 hard questions, so you can start easy and work up to the harder ones.
Company type
Hardware & semiconductors
Headquarters
Suwon, South Korea
Main India offices
Bengaluru, Noida, Delhi
Samsung C Programming with DSA interview process
Online coding test (Samsung competency test)
Technical interview 1
Technical interview 2
HR round
Typical pattern based on public candidate experiences. Rounds vary by role, team and year.
Solved for SamsungStart practicing
3 QUESTIONS FREESign up to unlock the full question bank and track your progress.Unlock every question and answer, then continue practising company-wise and package-wise.
volatile tells the compiler that a variable can change outside normal program flow, for example a hardware register, a variable changed in an interrupt handler or memory shared with another device. The compiler must read it from memory every time and cannot cache it in a register or remove repeated reads. It does not provide atomicity or thread safety on its own.
02
A recursive tree traversal crashes on a very large, skewed tree. How do you fix it?
A skewed tree has height close to n, so recursion depth becomes huge and overflows the call stack. Convert the traversal to an iterative version using an explicit stack allocated on the heap, or use Morris traversal for in-order with O(1) extra space. Long term, use a self-balancing tree so height stays around log n.
03
What is the difference between stack and heap allocation?
Stack memory holds local variables, is allocated and freed automatically when functions enter and return, and is very fast but limited in size, often 1 to 8 MB. Heap memory is allocated with malloc and freed manually, can be large and outlive the function, but is slower and can fragment or leak. Large or variable sized data and data that must outlive a function belong on the heap.
A memory leak happens when dynamically allocated memory is never freed and the last pointer to it is lost, so the program keeps growing in memory use. Long running services and embedded systems eventually fail. Detect leaks with Valgrind memcheck, AddressSanitizer with LeakSanitizer or allocation tracking wrappers. Prevent them with clear ownership rules and a single cleanup path per function.
05
Rotate an array to the right by k positions in O(n) time and O(1) space.
Use three reversals.
void rev(int *a, int l, int r) {
while (l < r) { int t = a[l]; a[l++] = a[r]; a[r--] = t; }
}
void rotate(int *a, int n, int k) {
k %= n;
if (k == 0) return;
rev(a, 0, n - 1);
rev(a, 0, k - 1);
rev(a, k, n - 1);
}
For 1 2 3 4 5 with k = 2 the result is 4 5 1 2 3.
06
Why are self-balancing trees like AVL and red-black trees needed?
A plain BST can degrade to a linked list with O(n) operations when data arrives in sorted order. AVL trees keep the height difference of subtrees at most one using rotations, giving faster lookups. Red-black trees use colour rules with looser balance, so inserts and deletes need fewer rotations. Both guarantee O(log n). Red-black trees are used in the Linux kernel, C++ std::map and Java TreeMap.
07
You need to find the shortest route between two cities on a map with distances. Which algorithm do you use and why?
Road distances are non-negative, so Dijkstra's algorithm with a min heap is the right choice, giving O((V + E) log V). If you only need one target, stop when it is popped from the heap. For very large maps, A star with a straight-line distance heuristic explores fewer nodes. If some edges had negative costs, Bellman-Ford would be needed.
08
Find the length of the longest common subsequence of two strings.
auto is the default for local variables, stored on the stack with block lifetime. register hints the variable should live in a CPU register, and its address cannot be taken. static gives a local variable lifetime for the whole program, or limits a global to its file. extern declares a variable defined in another file. Storage class decides scope, lifetime and linkage.
10
A program reads a file with while (!feof(fp)) and processes the last line twice. Why?
feof() becomes true only after a read has already failed at end of file. The loop checks feof before reading, so the final failed read leaves the old buffer content, which is processed again. Loop on the read function's return value instead, such as while (fgets(line, sizeof line, fp) != NULL), and check ferror after the loop.
The master theorem gives the time complexity of divide and conquer recurrences of the form T(n) = aT(n/b) + f(n). Compare f(n) with n to the power log base b of a. For merge sort, a = 2, b = 2 and f(n) = n, which match, giving O(n log n). For binary search, a = 1 and b = 2 with constant work, giving O(log n). It does not cover every recurrence.
Track the best sum ending here and the overall best.
long maxSubarray(const int *a, int n) {
long cur = a[0], best = a[0];
for (int i = 1; i < n; i++) {
cur = (cur + a[i] > a[i]) ? cur + a[i] : a[i];
if (cur > best) best = cur;
}
return best;
}
Input {-2, 1, -3, 4, -1, 2, 1, -5, 4} returns 6 for subarray 4, -1, 2, 1.
13
A strlen call crashes on a buffer received from a network packet. What is wrong?
Network data is not guaranteed to be null terminated, so strlen keeps reading past the buffer until it hits a zero byte or invalid memory. Always track the received byte count and use length-aware functions such as memchr, strnlen or memcpy with explicit sizes. If you need a C string, allocate one extra byte and add the terminator yourself after checking the length.
Use a slow pointer moving one step and a fast pointer moving two steps. If the list has a cycle, the fast pointer eventually meets the slow one inside the loop; if fast reaches NULL, there is no cycle. To find the cycle start, reset one pointer to head and move both one step at a time; they meet at the start. It uses O(n) time and O(1) space.
15
What is the difference between a macro and an inline function?
A macro is plain text substitution by the preprocessor, with no type checking, and arguments may be evaluated more than once, so SQUARE(i++) misbehaves. An inline function is a real function with type checks and single evaluation of arguments, which the compiler may expand in place for speed. Prefer static inline functions and use macros only for things like conditional compilation.
16
Implement union-find with path compression and union by rank.
Two small functions cover most uses.
int parent[MAXN], rnk[MAXN];
void makeSet(int n) {
for (int i = 0; i < n; i++) { parent[i] = i; rnk[i] = 0; }
}
int find(int x) {
if (parent[x] != x) parent[x] = find(parent[x]);
return parent[x];
}
int unite(int a, int b) {
a = find(a); b = find(b);
if (a == b) return 0;
if (rnk[a] < rnk[b]) { int t = a; a = b; b = t; }
parent[b] = a;
if (rnk[a] == rnk[b]) rnk[a]++;
return 1;
}
17
Evaluate a postfix expression of single digit numbers using a stack.
Push digits and apply operators to the top two values.
int evalPostfix(const char *s) {
int st[100], top = -1;
for (; *s; s++) {
if (isdigit((unsigned char)*s)) st[++top] = *s - '0';
else if (strchr("+-*/", *s)) {
int b = st[top--], a = st[top--];
switch (*s) {
case '+': st[++top] = a + b; break;
case '-': st[++top] = a - b; break;
case '*': st[++top] = a * b; break;
case '/': st[++top] = a / b; break;
}
}
}
return st[top];
}
23*5+ evaluates to 11.
18
Find the next greater element for every array element using a monotonic stack.
Keep indexes of elements still waiting for a greater value.
void nextGreater(const int *a, int n, int *res) {
int st[n], top = -1;
for (int i = 0; i < n; i++) {
res[i] = -1;
while (top >= 0 && a[st[top]] < a[i]) {
res[st[top--]] = a[i];
}
st[++top] = i;
}
}
For 4 5 2 25 the result is 5 25 25 -1, in O(n) time.
Adding an integer to a pointer moves it by that many elements, not bytes, so p + 1 on an int pointer advances by sizeof(int). Subtracting two pointers into the same array gives the number of elements between them as ptrdiff_t. Arithmetic is valid only within an array or one past its end; going further is undefined behaviour. Void pointer arithmetic is not allowed in standard C.
20
A program crashes with a segmentation fault. How do you find the cause?
Compile with -g and run under gdb; when it crashes, use bt to see the call stack and print variables in the failing frame. Common causes are dereferencing NULL, using freed memory, out-of-bounds array access and stack overflow from deep recursion. Building with -fsanitize=address gives a precise report of the bad access and where the memory was allocated or freed.
21
Why do linked lists perform poorly compared to arrays on modern CPUs?
Each node is allocated separately and may sit anywhere in memory, so traversing a list causes frequent cache misses and the CPU cannot prefetch well. Arrays store elements contiguously, so a cache line brings several elements at once. Even with O(1) insertion, linked lists are often slower in practice. Use them when frequent insertions in the middle with stable node addresses really matter.
22
How do you convert an infix expression to postfix?
Use the shunting yard algorithm with an operator stack. Output operands directly. For an operator, pop operators of higher precedence, or equal precedence if left associative, to the output, then push it. Push '(' and on ')' pop until the matching '('. At the end, pop all remaining operators. For example a + b * c becomes a b c * +. Postfix is easy to evaluate with a stack.
23
Why is a dummy or sentinel node useful in linked list code?
A dummy node placed before the real head removes special cases for an empty list or for inserting and deleting at the head, because every real node then has a previous node. Code for merging lists, removing elements and partitioning becomes shorter and less error prone. At the end, return dummy.next as the new head. It costs one extra node.
BFS explores level by level using a queue and finds the shortest path in unweighted graphs. DFS goes deep along one path using recursion or a stack, and suits cycle detection, topological sort, connected components and backtracking. Both run in O(V + E). BFS may use more memory on wide graphs, while recursive DFS can overflow the call stack on very deep graphs.
Split, sort halves, merge with a temp buffer.
void merge(int *a, int l, int m, int r, int *tmp) {
int i = l, j = m + 1, k = l;
while (i <= m && j <= r) tmp[k++] = (a[i] <= a[j]) ? a[i++] : a[j++];
while (i <= m) tmp[k++] = a[i++];
while (j <= r) tmp[k++] = a[j++];
for (k = l; k <= r; k++) a[k] = tmp[k];
}
void mergeSort(int *a, int l, int r, int *tmp) {
if (l >= r) return;
int m = l + (r - l) / 2;
mergeSort(a, l, m, tmp);
mergeSort(a, m + 1, r, tmp);
merge(a, l, m, r, tmp);
}
26
How do you check whether a linked list is a palindrome in O(1) extra space?
Find the middle with slow and fast pointers, reverse the second half in place, then compare the first half and the reversed second half node by node. Finally reverse the second half again to restore the list, which interviewers appreciate. Time is O(n) and extra space is O(1). A simpler O(n) space approach copies values into an array or stack first.
27
You need to implement a queue using two stacks. How does it work and what is its cost?
Use an in-stack for enqueue and an out-stack for dequeue. Enqueue always pushes to the in-stack. Dequeue pops from the out-stack, and only when it is empty moves all elements from the in-stack to it, which reverses their order so the oldest comes out first. Each element moves at most twice, so operations are O(1) amortised even though a single dequeue can be O(n).
28
Write a C program that counts lines, words and characters in a file.
Read character by character with fgetc.
FILE *fp = fopen(path, "r");
if (!fp) { perror("fopen"); return 1; }
long lines = 0, words = 0, chars = 0;
int c, inWord = 0;
while ((c = fgetc(fp)) != EOF) {
chars++;
if (c == '\n') lines++;
if (isspace(c)) inWord = 0;
else if (!inWord) { inWord = 1; words++; }
}
fclose(fp);
printf("%ld %ld %ld\n", lines, words, chars);
c is int so EOF can be distinguished from valid bytes.
29
Your embedded device must store 10,000 sensor records in limited RAM. How would you design the struct?
Use fixed-width types sized to the real range, such as uint16_t for readings and uint32_t for timestamps, order members from largest to smallest to cut padding and pack boolean flags into bit fields or a single bitmask byte. Consider storing time as an offset from a base to save bytes. Check sizeof in a static assertion so the size stays as designed.
30
A program that writes logs with fprintf loses the last lines when it crashes. Why, and how do you fix it?
stdio streams are buffered, so fprintf stores data in memory and writes it to disk only when the buffer fills, the file closes or fflush is called. A crash loses whatever is still in the buffer. Call fflush after important log lines, set line buffering with setvbuf(fp, NULL, _IOLBF, 0) or write critical messages to unbuffered stderr. For durability on disk, add fsync on POSIX systems.
Tips to crack the Samsung C Programming with DSA interview
Practise Samsung-style coding problems based on BFS, DFS, backtracking and simulation without using built-in libraries.
Revise C, C++ or Java fundamentals deeply along with operating systems and data structures.
Read about Samsung R&D work in India on Galaxy devices, One UI and on-device AI.
Keep a one-minute story ready for every project on your CV: problem, your part, tools, result.
Practise the hard questions on this page aloud; Samsung interviewers usually ask follow-ups on your first answer.
Frequently asked questions
How many rounds are there in the Samsung C Programming with DSA interview?
Candidates usually go through 4 stages: Online coding test (Samsung competency test); Technical interview 1; Technical interview 2; HR round. The exact rounds change with the role, team, location and your experience, so confirm the process with the recruiter.
Which topics should I prepare for the Samsung C Programming with DSA interview?
This practice set leans on Trees and Graphs (5), Pointers and Memory Management (4), Structures, Unions and File Handling (4), Linked Lists (4). Revise those first, then practise explaining one or two of your own projects end to end, because most Samsung interviewers ask you to walk through real work.
Are these the exact questions asked at Samsung?
No. These are curated practice questions that match the skills and difficulty usually tested in hardware & semiconductors interviews like Samsung's. They are not a leaked or verified list of past questions.
Where does Samsung hire C Programming with DSA professionals in India?
Samsung has large teams in Bengaluru, Noida, Delhi. Openings and locations change often, so check the Samsung careers page and filter by city and experience.
C Programming with DSA questions for similar companies