DCSIMG
Interview Question #2 - Recursive IsSubstring(s1,s2) - IHateSpaghetti {code}

IHateSpaghetti {code}

VSX, DSL and Beyond by Eyal Lantzman

Syndication

Coding / Architecture

Extensibility /DSL

Projects

Articles

Interview Question #2 - Recursive IsSubstring(s1,s2)

This post is a part of the Interview Question Series posts.

Check if the string s2 is a sub string of string s1 (when s2 is null or empty then return true)
You can use only string's indexer, string's Equals function, string's length function and string's substring function.
Use the following signature: bool IsSubstring(string s1, string s2) and solve in recursion.

Try to solve this by yourself first....

 

But anyway here's the solution for the problem:

There are two versions one of them is not that efficient (creates bunch of string instances) but more elegant.

Tip: solve using the elegant way and then suggest solving this in more efficient way or ask the interview what he prefers ;-)

 
   1: namespace Recursion2
   2: {
   3:   /// <summary>
   4:   /// Question:
   5:   /// Check if the string s2 is a substring of strign s1 (when s2 is null or empty then return true)
   6:   /// You can use only string indexer, string Equals, string length and string Substring.
   7:   /// Use the following signature: bool IsSubstring(string s1, string s2) and solve in recursion.
   8:   /// </summary>
   9:   class Program
  10:   {
  11:     /// <summary>
  12:     /// Test the solution
  13:     /// </summary>
  14:     /// <param name="args"></param>
  15:     static void Main(string[] args)
  16:     {
  17:       string[] s1s = new string[] { "aaabbbccc", "abc", "cba", "ddca", null, string.Empty };
  18:       string[] s2s = new string[] { "aa", "bb", "cc", "dd", "a", "b", "c", "d", "z", string.Empty, null };
  19:  
  20:       s1s.ToList().ForEach(
  21:         (s1) =>
  22:         {
  23:           s2s.ToList().ForEach(
  24:             (s2) =>
  25:             {
  26:               Console.WriteLine("Ver1: {0} is substring of {1} = {2}", s2 ?? "NULL", s1 ?? "NULL",IsSubstringVer1(s1,s2));
  27:               Console.WriteLine("Ver2: {0} is substring of {1} = {2}", s2 ?? "NULL", s1 ?? "NULL", IsSubstringVer2(s1, s2));
  28:             }
  29:           );
  30:         }
  31:       );
  32:  
  33:       Console.ReadLine();
  34:     }
  35:  
  36:     /// <summary>
  37:     /// Solution - Ver1
  38:     /// Con: Put high pressure on the GAC (create a lot of strings during the process)
  39:     /// Pro: No need to add additional funtions
  40:     /// </summary>
  41:     /// <param name="s1">string</param>
  42:     /// <param name="s2">sub string</param>
  43:     /// <returns>if s2 is s1's substing</returns>
  44:     public static bool IsSubstringVer1(string s1, string s2)
  45:     {
  46:       if (string.IsNullOrEmpty(s2))
  47:         return true;
  48:  
  49:       if (string.IsNullOrEmpty(s1))
  50:         return string.IsNullOrEmpty(s2);
  51:  
  52:       if (s2.Length == s1.Length)
  53:         return s2.Equals(s1);
  54:  
  55:       if (s2.Length > s1.Length)
  56:         return false;
  57:  
  58:       return IsSubstringVer1(s1.Substring(1), s2) //the first combination is to remove the first char from s1
  59:         || IsSubstringVer1(s1.Substring(0, s1.Length - 1), s2); //the second combination is to remove the last char from s2
  60:     }
  61:  
  62:     /// <summary>
  63:     /// Solution Ver2
  64:     /// Pro: No string allocations
  65:     /// Con: Extra "bootstraped" function
  66:     /// </summary>
  67:     /// <param name="s1"></param>
  68:     /// <param name="s2"></param>
  69:     /// <returns></returns>
  70:     public static bool IsSubstringVer2(string s1, string s2)
  71:     {
  72:       if (string.IsNullOrEmpty(s2))
  73:         return true;
  74:  
  75:       if (string.IsNullOrEmpty(s1))
  76:         return string.IsNullOrEmpty(s2);
  77:  
  78:       //bootstrap
  79:       return IsSubstringVer2(s1,1,0,s2,0);
  80:     }
  81:  
  82:     /// <summary>
  83:     /// Bootstrapped function
  84:     /// </summary>
  85:     /// <param name="s1"></param>
  86:     /// <param name="fallBackIndex">in case of missmach fallback to index</param>
  87:     /// <param name="s1Index">process s1 in index...</param>
  88:     /// <param name="s2"></param>
  89:     /// <param name="s2Index">process s2 in index...</param>
  90:     /// <returns></returns>
  91:     private static bool IsSubstringVer2(string s1, int fallBackIndex, int s1Index, string s2, int s2Index)
  92:     {
  93:       if (s2Index >= s2.Length)
  94:         return true; //if we reached here it means that s2 is substring of s1
  95:  
  96:       if (s1Index >= s1.Length)
  97:         return false; //if we reached here it means that s2 is not substring of s1
  98:  
  99:  
 100:       if (s1[s1Index] == s2[s2Index])
 101:         //if the current chars are the same proceed to the next
 102:         return IsSubstringVer2(s1, fallBackIndex, s1Index + 1, s2, s2Index + 1);
 103:  
 104:       //fall back and start over
 105:       return IsSubstringVer2(s1, fallBackIndex + 1, fallBackIndex, s2, 0);
 106:     }
 107:   }
 108: }

If you have found a bug or have a nicer way to solve this - share it.

Published Wednesday, February 04, 2009 10:00 AM by Eyal

Comments

# re: Interview Question #2@ Thursday, February 05, 2009 1:22 AM

Do you ask the candidates to write pseudo code or "code that compiles"? When I conduct interviews I usually like to see some C# but with people being used to auto-complete and such they usually end up with "pseudo-C#"...

# Audi Quattro S1@ Thursday, February 05, 2009 7:05 PM

Pingback from  Audi Quattro S1

# re: Interview Question #2@ Friday, February 06, 2009 2:39 AM

Hi Yaron,

I prefer C# and you can tell quite a lot about someone based on home much does he relies on intellisense. In general as long as the candidate is telling me his assumptions from a method (i.e substring(int i) is trimming from 0 to i) I can "forgive" some mistakes but it depends on the level required for a particular possition (Won't forgive to seniors C# devs).

by Eyal

# Suzuki Gs500e Tuning, Aftermarket Parts S500 Mercedes Benz Cl500@ Thursday, May 20, 2010 9:43 PM

Pingback from  Suzuki Gs500e Tuning, Aftermarket Parts S500 Mercedes Benz Cl500

# D810 American, 1936 Cord 810 Value@ Friday, May 21, 2010 4:25 AM

Pingback from  D810 American, 1936 Cord 810 Value

# 318is Discount Lowest Bmw 740i, 318i Electric@ Saturday, May 22, 2010 12:42 AM

Pingback from  318is Discount Lowest Bmw 740i, 318i Electric

# Sale 2001 Gmc Yukon Found, Yukon Power@ Saturday, May 22, 2010 1:30 AM

Pingback from  Sale 2001 Gmc Yukon Found, Yukon Power

# Crown Refurbished, E 450 Econoline Super Duty Accessories Explorer Sport Trac Crown Victoria@ Saturday, May 22, 2010 1:52 AM

Pingback from  Crown Refurbished, E 450 Econoline Super Duty Accessories Explorer Sport Trac Crown Victoria

# 1 Cars For Sale Mitsubishi L300, Replacement Toshiba Satellite L300 Dvd Supermulti@ Saturday, May 22, 2010 3:00 PM

Pingback from  1 Cars For Sale Mitsubishi L300, Replacement Toshiba Satellite L300 Dvd Supermulti

# Squareback Ready Vw, Squareback Sheet Music@ Saturday, May 22, 2010 11:04 PM

Pingback from  Squareback Ready Vw, Squareback Sheet Music

# Gmc Steel Cowl Induction Hoods, Remington Episode Television - 405.defutbolazo.com@ Monday, May 24, 2010 12:59 PM

Pingback from  Gmc Steel Cowl Induction Hoods, Remington Episode Television - 405.defutbolazo.com

# Tempest Blood, Pontiac Tempest Super Duty Cars - 413.tijuanareader.com@ Monday, May 24, 2010 11:49 PM

Pingback from  Tempest Blood, Pontiac Tempest Super Duty Cars - 413.tijuanareader.com

# 265wt Price, Tires 265 75 R20 - 33.codebluehacks.org@ Tuesday, May 25, 2010 12:49 AM

Pingback from  265wt Price, Tires 265 75 R20 - 33.codebluehacks.org

# 2000 Mazda Mx 5 Miata Convertible Porsche Boxster, 1990 Mazda Miata Convertible - 75.cmanager.org@ Tuesday, May 25, 2010 11:16 AM

Pingback from  2000 Mazda Mx 5 Miata Convertible Porsche Boxster, 1990 Mazda Miata Convertible - 75.cmanager.org

# 2009 Malibu Ss, Malibu Grill Bbq - 285.binggreen.com@ Tuesday, May 25, 2010 1:17 PM

Pingback from  2009 Malibu Ss, Malibu Grill Bbq - 285.binggreen.com

# Bronco Episode, 95 Bronco Performance - 329.myipgirl.com@ Tuesday, May 25, 2010 1:28 PM

Pingback from  Bronco Episode, 95 Bronco Performance - 329.myipgirl.com

# Super Beetle Cheats, Buy Beetle Eurovan Volkswagen Touareg - 266.renters.ws@ Tuesday, May 25, 2010 8:20 PM

Pingback from  Super Beetle Cheats, Buy Beetle Eurovan Volkswagen Touareg - 266.renters.ws

# Mercury Bobcat Parts Oil Pan Grand Marquis, Window Regulator Replacement Mercury Marquis My Grand - 30.tijuanareader.com@ Tuesday, May 25, 2010 9:44 PM

Pingback from  Mercury Bobcat Parts Oil Pan Grand Marquis, Window Regulator Replacement Mercury Marquis My Grand - 30.tijuanareader.com

# 400sel Factory Left, 400sel Info Sale - 19.an74.com@ Tuesday, May 25, 2010 10:01 PM

Pingback from  400sel Factory Left, 400sel Info Sale - 19.an74.com

# 2000 Mazda 626 Horsepower Specifications, Radiator Subaru Gl 10 Parts Power Steering Pump Shock Absorber - 348.cmanager.org@ Wednesday, May 26, 2010 5:44 AM

Pingback from  2000 Mazda 626 Horsepower Specifications, Radiator Subaru Gl 10 Parts Power Steering Pump Shock Absorber - 348.cmanager.org

# 1992 - 1989 @ Ford Escape Hitch, 2002 Ford Escape Egr Valve Sensor - 188.jeepsunlimted.com@ Monday, May 31, 2010 5:13 AM

Pingback from  1992 - 1989 @ Ford Escape Hitch, 2002 Ford Escape Egr Valve Sensor - 188.jeepsunlimted.com

# 1994 - 1987 @ Radon G4 Bulb 20w 12v Contemporary, Wholesale 1993 Aerostar Ford Tempo - 132.tvshowzone.com@ Monday, May 31, 2010 10:46 PM

Pingback from  1994 - 1987 @ Radon G4 Bulb 20w 12v Contemporary, Wholesale 1993 Aerostar Ford Tempo - 132.tvshowzone.com

# re: Interview Question #2 - Recursive IsSubstring(s1,s2)@ Saturday, July 10, 2010 6:34 PM

For as the rain cometh down, and the snow from heaven, and returneth not thither, but watereth the earth, and maketh it bring forth and bud, that it may give seed to the sower, and bread to the eater:

# re: Interview Question #2 - Recursive IsSubstring(s1,s2)@ Friday, February 25, 2011 7:28 PM

alman90210|0:28-0:31п»ї wtf hes meeting his guild in ironforge but hes a lowbie tauren? :o

 http://mp3musicsite.com/?p=99

thplousaqq

by ToratordDrale

# re: Interview Question #2 - Recursive IsSubstring(s1,s2)@ Friday, February 25, 2011 8:54 PM

Ravenclaw160|that is brilliant. A+! and is that a newп»ї band member?

 canadianmedstore.in

thplousaqq

by ToratordDrale

Leave a Comment

(required) 
(required) 
(optional)
(required) 

Enter the numbers above: