Hello Everyone,
I am currently interviewing for Embedded/Firmware-related jobs. Strings are not my strongest suit. So far, I have been asked to check if a given string is a palindrome or not. However, since I started doing LeetCode, I have been struggling the most with string-related questions, especially those involving substrings.
What have been the most common interview questions you've been asked, particularly those involving strings?
Videos
Howdy! I have an interview coming up in 2 weeks and wanted to practice string manipulation(as it was hinted that’s what I’d be tested on).
I’ve practiced a few already but mostly been seeing similar 2 pointer ones. Does anyone have any other questions they recommend to just practice string manipulation or string parsing that isn’t the typical reverse a string?
I’m trying to compile a few more problems to practice and hope to be ready.
Both results in undefined behaviour.
The first is because you are using a pointer (str) after free'ing it (free() doesn't/can't set the pointer to NULL after being the block).
The second because you are using a pointer to a local variable from another function.
Both examples contain the same type of error, using an object after its lifetime has ended.
Doing so results in undefined behavior1. The text uses the word 'referred', which basically means accessing an object or using an identifier of said object.
What actually causes undefined behavior in both examples isn't the rule mentioned above1, but another one, which is closely related. The value of a pointer which points to an object whose lifetime has ended is indeterminate2. Reading such values causes undefined behavior. This happens in both examples.
In the first example, the lifetime of str ends at the call to free. The pointer is then used in the if statement if(str != NULL){ which causes undefined behavior.
In the second example, the lifetime of p ends when the function returns. The returned pointer is assigned to a pointer str: str = GetMemory();, which causes undefined behavior.
1 (Quoted from: ISO/IEC 9899:201x 6.2.4 Storage durations of objects 2)
If an object is referred to outside of its
lifetime, the behavior is undefined.
2 (Quoted from: ISO/IEC 9899:201x 6.2.4 Storage durations of objects 2)
The value of a pointer becomes indeterminate when
the object it points to (or just past) reaches the end of its lifetime.
They might ask you about regular expressions. If they are using Java, they might ask the difference of StringBuffer and StringBuilder.
Reverse words in a sentence, e.g.
"string manip interview question"
becomes
"question interview manip string"
this has a solution that uses only one char worth of temporary space.