• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to secondary sidebar

GoHired

Interview Questions asked in Google, Microsoft, Amazon

Join WeekEnd Online Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

  • Home
  • Best Java Books
  • Algorithm
  • Internship
  • Certificates
  • About Us
  • Contact Us
  • Privacy Policy
  • Array
  • Stack
  • Queue
  • LinkedList
  • DP
  • Strings
  • Tree
  • Mathametical
  • Puzzles
  • Graph

Given Set of words or A String find whether chain is possible from these words or not

August 25, 2014 by Dhaval Dave

You are given N strings or N words. You have to find whether a chain can be formed with all the strings given namely n ?
Chain : A chain can be formed between 2 strings if last character of the 1st string matches with the first character of the second string.
Example : cat ,eng, galactic, tree
These words Create chain as  …  TREE,ENG,GALACTIC,CAT or CAT, TREE, ENG, GALACTIC
Try to think as its simple solution.

Logic 1)
Brute Force Approach : With each word from input string and try to create chain.
example 
Input : English, Yellow , Maths , Chemistry , Water , Horse
Take word  “English” which ends with “H” Find another word which starts with H.
Like wise continue and if you can’t find chain containing all words return “failure”
else return “true”
Logic 2) 
Create Two array for Input words
Arr1 = Contains only first character of all words
Arr2 = Contains only last character of all words
Now check if all occurrence of Arr1 are part of Arr2 or not.
If they are then they create Chain  else not.
Example 1) 
For Input English, Yellow , Maths , Chamistry , Water , Horse
Arr1 = { e , y , m , c , w , h}
Arr2 = { h , w , s , y , r , e }
Example 2)
For Input cat ,engg, galactic, tree
Arr1 = { c , e , g , t }
Arr2 = { t , g , c , e }
Example 3) : 
For Input : Apple, elephant, truck.
Also creates Chain and not ring.
Arr1 = { a , e , t }
Arr2 = { e,  t , k }
Modification : 
Only One character from Arr1 and one from Arr2 can be considered if only if they both are not from one word.
means here apart from  ‘a’ of Arr1 and ‘k’ of Arr2 we can eliminate for occurrence count if they both are not from one word.
say words are ark, elephant, tree will also create same Arr1 and Arr2 as above Example3) but as “ark” contains both starting and ending character which doesn’t meet. This Words cant create chain.
Code  :
 Its Very Simple code Please try at your own.
Thanks Dhaval for suggesting this Approach and  article.

Similar Articles

Filed Under: Amazon Interview Question, Flipkart Interview Questions, Interview Questions, Microsoft Interview Questions, problem Tagged With: Dynamic Programming, string

Reader Interactions

Primary Sidebar

Join WeekEnd Online/Offline Batch from 4-April-2020 on How to Crack Coding Interview in Just 10 Weeks : Fees just 20,000 INR

Join WeekEnd Online/Offline Batch from 4-April-2020

WhatsApp us

Secondary Sidebar

Custom Search

  • How I cracked AMAZON
  • LeetCode
  • Adobe
  • Amazon
  • Facebook
  • Microsoft
  • Hacker Earth
  • CSE Interview

Top Rated Questions

Minimum insertions to form a palindrome

SAP Off Campus Hiring_ March 2015 Sample Questions

Amazon Interview On-Campus For Internship – 1

N teams are participating. each team plays twice with all other teams. Some of them will go to the semi final. Find Minimum and Maximum number of matches that a team has to win to qualify for finals ?

Find two non repeating elements in an array of repeating elements

Add Sub Multiply very large number stored as string

Flipkart Set 1 On Campus with Answers

N Petrol bunks or City arranged in circle. You have Fuel and distance between petrol bunks. Is it possible to find starting point so that we can travel all Petrol Bunks

Given a string, find the first character which is non-repetitive

Client Server C program

Possible sizes of bus to carry n groups of friends

C Program for TAIL command of UNIX

Binary Tree Isomorphic to each other

Stock Buy Sell to Maximize Profit

Fibonacci Hashing & Fastest Hashtable

Check Binary Tree is Binary Search Tree or not

Right view of Binary tree

Leetcode: Merge Intervals

‘N’ Story Building, with 1,2,3 steps how many ways can a person reach top of building.

Subset Sum Problem Dynamic programming

Knight Tour Problem (Graph – Breadth First Search)

Regular Expression Matching

Find the kth number with prime factors 3, 5 and 7

TicTacToe Game As Asked in Flipkart

Maximum path sum between two leaves

Printing intermediate Integers between one element & next element of array

Reversal of LinkedList

Maximum size of square sub matrix with all 1’s in a binary matrix

LeetCode: Container With Most Water

Find Percentage of Words matching in Two Strings

Copyright © 2026 · Genesis Framework · WordPress · Log in