Message on Whatsapp 8879355057 for DSA(OA + Interview) + Fullstack Dev Training + 1-1 Personalized Mentoring to get 10+LPA Job
0 like 0 dislike
3,517 views
in Online Assessments by Expert (108,110 points) | 3,517 views

2 Answers

0 like 0 dislike
Best answer

HackerRank | 2 Questions | 120 mins

 

Q1) Given a list of integers, perform cardinality sort, where cardinality of a number is the count of all set bits in that number. If cardinality is same, sort in ascending order. Return the list after sorting.

 

https://leetcode.com/problems/sort-integers-by-the-number-of-1-bits/

 

Algorithm:

 

  1. Calculate cardinality using hamming weight algorithm
  2. Traverse list to store number and cardinality as pair in min heap
  3. Iterate min heap for result.

 

All test cases passed.

 

Q2) Given an array of words and an array of sentences, determine which words are anagrams of each other. Calculate how many sentences can be created by replacing any word with one of the anagrams. Return a list containing number of sentences possible for each sentence.

 

Example
wordSet = ['listen', 'silent, 'it', 'is']
sentence = 'listen it is silent'

 

Determine that listen is an anagram of silent. Those two words can be replaced with their anagrams.
The four sentences that can be created are:
• listen it is silent
• listen it is listen
• silent it is silent
• silent it is listen

 

https://leetcode.com/discuss/interview-question/1541093/How-many-sentences-Can-someone-provide-a-python-solution-for-this-question

 

Intuition
In the given example, anagramns are - { (listen, silent), (it), (is) }
For the given sentence, 'listen it is silent', there can be 2 replacements for both 'listen' and 'silent'.

 

						listen it is silent
		Combinations = 	2 * 1 * 1 * 2    =    4

 

Calculate number of anagrams for each word using frequency map.
Iterate each sentence to calculate the combinations.

 

All testcases passed.

by Expert (108,110 points)
0 like 0 dislike
by Expert (108,110 points)

Get best answers to any doubt/query/question related to programming , jobs, gate, internships and tech-companies. Feel free to ask a question and you will receive the best advice/suggestion related to anything you ask about software-engineering , development and programming problems .