Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 958351
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T00:51:02+00:00 2026-05-16T00:51:02+00:00

I was going through some Amazon interview questions on CareerCup.com, and I came across

  • 0

I was going through some Amazon interview questions on CareerCup.com, and I came across this interesting question which I haven’t been able to figure out how to do. I have been thinking on this since 2 days. Either I am taking a way off approach, or its a genuinely hard function to write.

Question is as follows:

Write a function in C that can find if a string is a sub-string of another. Note that a mismatch of one character
should be ignored.

A mismatch can be an extra character: ’dog’ matches ‘xxxdoogyyyy’  
A mismatch can be a missing character: ’dog’ matches ‘xxxdgyyyy’ 
A mismatch can be a different character: ’dog’ matches ‘xxxdigyyyy’

The return value wasn’t mentioned in the question, so I assume the signature of the function can be something like this:

char * MatchWithTolerance(const char * str, const char * substr);

If there is a match with the given rules, return the pointer to the beginning of matched substring within the string. Else return null.

Bonus

If someone can also figure out a generic way of making the tolerance to n instead of 1, then that would be just brilliant.
In that case the signature would be:

char * MatchWithTolerance(const char * str, const char * substr, unsigned int tolerance = 1);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T00:51:03+00:00Added an answer on May 16, 2026 at 12:51 am

    This seems to work, let me know if you find any errors and I’ll try to fix them:

    int findHelper(const char *str, const char *substr, int mustMatch = 0)
    {
        if ( *substr == '\0' )
            return 1;
    
        if ( *str == '\0' )
            return 0;
    
        if ( *str == *substr )
            return findHelper(str + 1, substr + 1, mustMatch);
        else
        {
            if ( mustMatch )
                return 0;
    
            if ( *(str + 1) == *substr )
                return findHelper(str + 1, substr, 1);
            else if ( *str == *(substr + 1) )
                return findHelper(str, substr + 1, 1);
            else if ( *(str + 1) == *(substr + 1) )
                return findHelper(str + 1, substr + 1, 1);
            else if ( *(substr + 1) == '\0' )
                return 1;
            else
                return 0;
        }
    }
    
    int find(const char *str, const char *substr)
    {
        int ok = 0;
        while ( *str != '\0' )
            ok |= findHelper(str++, substr, 0);
    
        return ok;
    }
    
    
    int main()
    {
        printf("%d\n", find("xxxdoogyyyy", "dog"));
        printf("%d\n", find("xxxdgyyyy", "dog"));
        printf("%d\n", find("xxxdigyyyy", "dog"));
    }
    

    Basically, I make sure only one character can differ, and run the function that does this for every suffix of the haystack.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I was going through some interview questions and I came across one about
I was going through some code and came across a scenario where my combobox
I was just going through some codes of C++. Where in I came across
I'm going through some old stored procedures at work and constantly come across CASE
I'm going through some book which tries to teach android development. In the book,
I was just going through some security blogs and I found this image of
If I would like to process payments directly without going through Google / Amazon
So I'm (still) going through some slow legacy sql views used to do calculate
I'm thinking about starting a new project using EF 4 and going through some
So I am going through our database and removing some unneeded access levels that

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.