Linkedin Interview Question for Software Engineer / Developers






Comment hidden because of low score. Click to expand.
6
of 6 vote

Option 1:
-> take an array of a[26], count the occurrences of all characters in the text you need and the total number of letters.
-> now while parsing the magazine just access a[character] and reduce it by 1 only if a[character] > 0 and --totalNumOfLetters. when totalNumLetters == 0 you got the answer

Option 2:
-> Learn Muay Thai and beat the s**t out of the gunman

Option 3:
-> Kiss the gunman and Kick his b**ls

- Anonymous August 13, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the 3rd option is what the interviewer had in mind.

- mbriskar October 12, 2015 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

This would probably work with an assumption that the character set is ascii

public boolean ransom(String note, String magazine){
		int[] hash = new int[256];
		char[] mag = magazine.toCharArray();
		char[] noteArr = note.toCharArray();
		for(char alphabet : mag){
			hash[alphabet]++;
		}
		
		for(char check : noteArr){
			if(hash[check] <= 0){
				return false;
			}
			else {
				hash[check]--;
			}
		}
		
		return true;
	}

- abhishek.veldurthy April 04, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Suppose the magazine is really long, there is no need to build a hash map to the entire magazine.

- goalchaser December 03, 2014 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Why not make a hash of the note so that your algo will stop if all the note's characters are covered. This way you dont have to scan the entire magazine and you can stop early

- GA July 10, 2015 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

1) Start traversing the ransom note and text simultaneously.
2) Maintain an ASCII character map for magazine text.
3) For each character in ransom note (except space) check if we have found the character from text by checking it's count (>=1). If it is (>=1) decrement it by 1 and move to next character in text.
4) If we haven't found the char in magazine text yet, keep parsing it (magazine) and keep adding newly found characters to map until you find the character you were looking for.

The worst case complexity will be O(m+n) when we can't make ransom out of text or if we are unlucky otherwise we would not have to traverse entire magazine and will be done much earlier. For ex: I ran the following code on a pattern of length 50 and text 1000, I had to parse only 204 characters (out of 1000) before reaching the answer.

bool makeRansomNote(string ransomNote, string text)
{
   int ASCII[26]={0};
   std::transform(ransomNote.begin(), ransomNote.end(),ransomNote.begin(),::tolower);
   std::transform(text.begin(), text.end(),text.begin(),::tolower);

   int pos=0;
   for(int i=0; i<ransomNote.length(); i++)
   {
        if(ransomNote[i]!= ' ')
        {
                while(pos<text.length() && ASCII[ransomNote[i]-97]==0)
                {
                        if(text[pos]==' ') { pos++; continue;}
                        ASCII[text[pos++]-97]++;
                }
                if(ASCII[ransomNote[i]-97]>=1 && pos<text.length())
                {
                        ASCII[ransomNote[i]-97]-=1;
                }
                else return false;
        }
   }
   return true;
}

- Second Attempt March 03, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

^^ Its A is a subsequence of D , not substring

- Anon September 06, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

not a subsequence, order is not important

- Anonymous September 23, 2011 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Use trie.
cost of populating the Trie is O(m* log|x|) where m is the distinct number of words in the magazine. |X| = avg length of words in the magazine

cost of finding the match of the words of the input sentence is n*log(|X|) where n = number of words in the input sentence.

- Anonymous February 17, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is Longest subsequence problem.
Longest subsequence should be of size of sentence.

- AJ November 11, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

a fancy(retarded?) way of asking 'Is A a sub-string of B'... sighh these D-bag interviewers! **rolls eyes**

- code-monkey August 15, 2011 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This is not a 'Is A a sub-string of B' problem.
Example: If you need 'The roll' and if the magazine says 'The dog loves rolling' our answer to the problem would still be yes.

- Arxo Clay November 03, 2011 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

1, create linklist for all distinct characters in the sentence, and keep track the number of them;
2, for the incoming magazine string, only keep track the occurring number of the characters in the previous link list;
3, if all corresponding numbers form the magazine are greater or equal to the previous linklist, report success.

- xbai@smu.edu August 12, 2011 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More