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 8355355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T09:50:11+00:00 2026-06-09T09:50:11+00:00

Recently I had an interview and I was asked to write a algorithm to

  • 0

Recently I had an interview and I was asked to write a algorithm to find the minimum number of 1 letter changes to get from a particular of word to a given word , i.e. Cat->Cot->Cog->Dog

I dont want the solution of the problem just guide me through How I can use BFS in this algorithm ?

  • 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-06-09T09:50:13+00:00Added an answer on June 9, 2026 at 9:50 am

    according to this scrabble list, the shortest path between cat and dog is:
    [‘CAT’, ‘COT’, ‘COG’, ‘DOG’]

    from urllib import urlopen
    
    def get_words():
        try:
            html = open('three_letter_words.txt').read()
        except IOError:
            html = urlopen('http://www.yak.net/kablooey/scrabble/3letterwords.html').read()
            with open('three_letter_words.txt', 'w') as f:
                f.write(html)
    
        b = html.find('<PRE>') #ignore the html before the <pre>
        while True:
            a = html.find("<B>", b) + 3
            b = html.find("</B>", a)
            word = html[a: b]
            if word == "ZZZ":
                break
            assert(len(word) == 3)
            yield word
    
    words = list(get_words())
    
    def get_template(word):
        c1, c2, c3 = word[0], word[1], word[2]
        t1 = 1, c1, c2
        t2 = 2, c1, c3
        t3 = 3, c2, c3
        return t1, t2, t3
    
    d = {}
    for word in words:
        template = get_template(word)
        for ti in template:
            d[ti] = d.get(ti, []) + [word] #add the word to the set of words with that template
    
    for ti in get_template('COG'):
        print d[ti]
    #['COB', 'COD', 'COG', 'COL', 'CON', 'COO', 'COO', 'COP', 'COR', 'COS', 'COT', 'COW', 'COX', 'COY', 'COZ']
    #['CIG', 'COG']
    # ['BOG', 'COG', 'DOG', 'FOG', 'HOG', 'JOG', 'LOG', 'MOG', 'NOG', 'TOG', 'WOG']
    
    import networkx
    G = networkx.Graph()
    
    for word_list in d.values():
        for word1 in word_list:
            for word2 in word_list:
                if word1 != word2:
                    G.add_edge(word1, word2)
    
    print G['COG']
    #{'COP': {}, 'COS': {}, 'COR': {}, 'CIG': {}, 'COT': {}, 'COW': {}, 'COY': {}, 'COX': {}, 'COZ': {}, 'DOG': {}, 'CON': {}, 'COB': {}, 'COD': {}, 'COL': {}, 'COO': {}, 'LOG': {}, 'TOG': {}, 'JOG': {}, 'BOG': {}, 'HOG': {}, 'FOG': {}, 'WOG': {}, 'NOG': {}, 'MOG': {}}
    
    print networkx.shortest_path(G, 'CAT', 'DOG')
    ['CAT', 'OCA', 'DOC', 'DOG']
    

    As a bonus we can get the farthest:

    print max(networkx.all_pairs_shortest_path(G, 'CAT')['CAT'].values(), key=len)
    #['CAT', 'CAP', 'YAP', 'YUP', 'YUK']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Recently in an interview I was asked to write a query where I had
This task has already been asked/answered, but I recently had a job interview that
I had an interview recently and he asked me about Singleton Design Patterns about
I had a C++ interview recently where I was asked, how does the compiler
I had an interview recently where I was asked to produce the traditional FizzBuzz
Recently i had an interview in which the question was asked as How would
I recently had this question in an interview to write test cases to test
Recently I had a question on the interview - I was asked to compare
I recently had an interview where I was asked to connect to the companies
I recently had my free application published in Mac Appstore. I'm trying to find

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.