Mid From PDF Coding C# Coding Interview

Find the Longest Palindromic Substring?

Short answer: string LongestPalindrome(string s) { if (string.IsNullOrEmpty(s)) return ""; int start = 0, maxLen = 1; for (int i = 0; i < s.Length; i++) { ExpandAroundCenter(s, i, i, ref start, ref maxLen); // Odd length palindrome ExpandAroundCenter(s, i, i + 1, ref start, ref maxLen); // Even length palindrome } return s.Substring(start, maxLen); } void ExpandAroundCenter(string s, int left, int right, ref int start, ref int…

Explain a bit more

maxLen) { while (left >= 0 && right < s.Length && s[left] == s[right]) { if (right - left + 1 > maxLen) Follow on: { start = left; maxLen = right - left + 1; } left--; right++; } } Explanation: Expand around each center to find the longest palindrome in O(n²).

Example code

string LongestPalindrome(string s)
{
if (string.IsNullOrEmpty(s)) return "";
int start = 0, maxLen = 1;
for (int i = 0; i < s.Length; i++)
{ ExpandAroundCenter(s, i, i, ref start, ref maxLen); // Odd length palindrome ExpandAroundCenter(s, i, i + 1, ref start, ref maxLen); // Even length palindrome }
return s.Substring(start, maxLen);
} void ExpandAroundCenter(string s, int left, int right, ref int start, ref int maxLen) { while (left >= 0 && right < s.Length && s[left] == s[right]) {
if (right - left + 1 > maxLen) Follow on: {
start = left;
maxLen = right - left + 1;
} left--; right++; }
} Explanation: Expand around each center to find the longest palindrome in O(n²).

Real-world example (ShopNest)

In coding rounds, state complexity aloud, write a clear ShopNest-flavored example (orders, carts), then handle edge cases (empty list, null, overflow).

Say this in the interview

  1. Define — one clear sentence (the short answer above).
  2. Example — relate it to a project like ShopNest or your real work.
  3. Trade-off — when you would not use it.
Toolliyo Assistant
Ask about tutorials, ebooks, training, pricing, mentor services, and support. I use public site content only—not admin or internal tools.

care@toolliyo.com

Need callback? Share your details