Strings in C
- Walk a NUL-terminated buffer by pointer without reading past it or moving the terminator
- Bound a substring scan so an unsigned length can never wrap
- Parse a 32-bit integer by hand and catch overflow before it happens
Intel's C round often takes away and asks you to write the short library functions yourself. Writing strlen means walking a pointer to the NUL byte, and the bugs show up around that walk. A scan reads one byte past its buffer, or an int wraps while digits are still arriving. Both mistakes compile cleanly and pass the happy-path test. The interviewer will look for them.
Why ask this? Below the operating system, the library often isn't there. Boot firmware runs before any C runtime exists, and a driver is often built freestanding with only the headers it brings along. So the same few hand-written routines come up again and again in the C round and on the firmware track. Each one takes about fifteen minutes, and most of that time goes to the edge cases.
strstr and strtol each have a documented contract, and Intel's follow-ups tend to check your version against it. An empty needle matches at index 0, because that's what strstr returns. An out-of-range number in strtol clamps and sets errno instead of wrapping. Firmware and driver engineers at Intel write these routines for real when no runtime is linked, so they notice quickly when yours disagrees with the contract.The NUL byte and the length you do not have
A C string carries no length. It is a run of char that ends at the first zero byte, and every routine in finds that byte by walking to it. When you write the walk yourself, two things follow. Measuring costs a full pass every time, so a loop that calls strlen in its condition turns a linear scan quadratic. The terminator also belongs to the buffer without belonging to the string, so anything that rearranges characters has to stop one short of it.
// strlen by hand: walk to the terminator, return the distance walkedsize_t my_strlen(const char *s) { const char *p = s; while (*p != '\0') p++; // stop ON the NUL, never past it return (size_t)(p - s); // pointer difference is the length}Lengths in C are size_t, and size_t cannot go negative. With a needle longer than the haystack, n - m does not come out negative. It wraps to a number near 2^64, and a loop guarded by i <= n - m runs off the end of the buffer. Compare before you subtract (if (m > n) return -1;) or write the bound as i + m <= n, which never underflows.
Asked at Intel
Three problems, each a library routine written without the library: reversal as a warm-up, then substring search, then the integer parse that firmware interviews pair with malloc and byte swapping. Read each answer in C or C++, the language you'll write in the round, then run the Python model on the empty and overflowing inputs before you explain it out loud.
Reverse a String In Place: Two Pointers, No Library
Problem
s of ASCII characters, reverse it in place and return the result. Swap characters from the two ends inward with a pair of indexes. Do not call a library reverse, do not slice backward, and do not copy into a second buffer.For this exercise the string arrives as a string and you return the reversed string, but the work that matters is the swap on a char buffer underneath. In C that buffer ends in a NUL byte, you find its length by walking to that byte, and the NUL never moves. Aim for O(n) time and O(1) extra space.
h swaps with o, e swaps with the second l, and the middle l stays where it is.
Two characters trade places in a single swap, then the indexes cross and the loop stops.
An empty string has nothing to swap, so it comes back unchanged.
Put one index on the first character and one on the last. Swap the two characters, step both indexes toward the middle, and stop as soon as they meet or cross.
Reverse a String In Place: Two Pointers, No Library
Click "Run" to execute your code
Substring Search: Find the Needle by Walking Pointers
Problem
haystack and needle, return the index where needle first occurs inside haystack, or -1 if it never does. Write the comparison yourself, one character at a time. Library calls such as strstr, find, indexOf or Python's in are off the table, because the exercise is the scan they hide.An empty
needle matches at index 0, the same answer C's strstr gives when it hands back the haystack itself. Some versions of the question ask only whether needle is present, and returning the index answers that too. Aim for O((n - m + 1) * m) time with O(1) extra space, and be ready to explain why the outer loop stops where it does.The window that starts at index 2 reads "ll".
The needle is longer than the haystack, so there is no legal start at all.
An empty needle matches before the first character, as strstr does.
Slide a start index i from 0 up to n - m inclusive. At each start, walk a second index j across the needle while the characters agree. If j reaches m, every character matched and i is the answer.
Substring Search: Find the Needle by Walking Pointers
Click "Run" to execute your code
String to Integer (atoi): Clamp Before the Multiply
Problem
my_atoi(s), which turns a string into a 32-bit signed integer the way C's atoi family does, with no library parsing call. Skip leading space characters. A tab or newline counts as an ordinary character and ends the parse. Read at most one + or -. Then read digits until the first non-digit or the end of the string, and ignore whatever follows. If no digit was read, return 0.If the value falls outside the range
-2147483648 to 2147483647, clamp it to the nearer end, which is how strtol saturates. Your accumulator may never pass through an overflowed value on the way there, so the range test has to happen before the multiply. Aim for one left-to-right pass, O(n) time and O(1) extra space.Three spaces are skipped, the minus sets the sign, and the leading zero adds nothing.
Parsing stops at the first non-digit, the c, and the rest of the string is ignored.
The magnitude passes 2147483648 at the tenth digit, so the result clamps to INT_MIN.
Move one index through four phases in order: spaces, an optional sign, digits, stop. Before each result * 10 + d, compare result against INT_MAX / 10 and clamp right there if the next step would leave the range.
String to Integer (atoi): Clamp Before the Multiply
Click "Run" to execute your code
INT_MAX / 10 before you multiply. Then expect a question about the caller, since a bare return value cannot tell a parsed "0" from garbage.int parse_i32(const char *s, int32_t *out). Report "no digits" and "out of range" as distinct errors. The parse stays one pass, and the only new state is where the digits started.Key Takeaways
- ✓A C string is a run of
charending at a zero byte, with no stored length. Find the end by walking a pointer, keep the terminator where it is, and remember that every measurement costs a full pass. - ✓Reverse in place with two pointers that meet in the middle, swapping only below the terminator. The follow-up reverses the words of a sentence by reversing the whole buffer and then each word, still in O(1) extra space.
- ✓Substring search stops at the last start where the needle still fits, and an empty needle matches at 0. Guard the
n - mbound against unsigned wraparound, and describe KMP as the linear-time answer without writing it unasked. - ✓Parse an integer in one pass and clamp before the multiply, because signed overflow in C is undefined and the compiler may delete a check that runs afterward. Data Structures in C comes next.
Citing this page?How we research these guides
Intel interview guideKnow what Intel asks before you walk in.
26 coding · 3 system design · 13 behavioral questions
Get their questions first.
InterviewLegend is independent and not affiliated with or endorsed by any company it covers.