Ollivanders Inventory Answer

In the context of a magical world, wands are essential tools for wizards. Each wand has certain properties like power, age, and cost. However, not all wands are good; some are evil. The problem here is to find the least expensive non-evil wands for each combination of power and age, and sort the result by power and age in descending order.

Secure your next interview 🎯

CodeRankGPT is a tool powered by GPT-4 that quietly assists you during your coding interview, providing the solutions you need.
In real-time and absolutely undetectable 🥷

Here is the Ollivanders Inventory Answer HackerRank solution using CodeRankGPT:

The solution uses a SQL SELECT statement to retrieve the desired data. It joins the 'wands' table with the 'wands_property' table based on the 'code' field. It then filters out the evil wands using a WHERE clause. To find the least expensive wand for each power and age combination, it uses a subquery in the WHERE clause that returns the minimum 'coins_needed' for each 'power' and 'age' combination. Finally, it sorts the result by 'power' and 'age' in descending order using the ORDER BY clause.


SELECT a.id, 
       b.age, 
       a.coins_needed, 
       a.power 
FROM   wands a 
       JOIN wands_property b 
         ON a.code = b.code 
WHERE  b.is_evil = 0 
       AND a.coins_needed = (SELECT Min(a1.coins_needed) 
                             FROM   wands a1 
                                    JOIN wands_property b1 
                                      ON a1.code = b1.code 
                             WHERE  b.age = b1.age 
                                    AND a.power = a1.power) 
ORDER  BY a.power DESC, 
          b.age DESC;

If you have a HackerRank coding test coming up, you can use CodeRankGPT to your advantage. It will assist you during your interview and help ensure you get the job.

AI is here now, and other candidates might be using it to get ahead and win the job. 🧐

Built on Unicorn Platform