Created
March 27, 2025 17:34
-
-
Save RockyGitHub/64640c53dc40215ca84bd28e5f3b27e5 to your computer and use it in GitHub Desktop.
interview question
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Dragon Treasure Hoard Manager | |
// | |
// Problem Statement: | |
// You're writing software for dragons to manage their treasure hoards. | |
// Implement a system that allows dragons to: | |
// 1. Add different types of treasures to their hoard (gold coins, gems, magical artifacts) | |
// 2. Calculate the total value of their hoard | |
// 3. Find the rarest items in their collection | |
// 4. Trade items with other dragons | |
use std::collections::HashMap; | |
// Types of treasures a dragon can collect | |
enum TreasureType { | |
GoldCoin, | |
Gem, | |
MagicalArtifact, | |
} | |
struct Treasure { | |
name: String, | |
treasure_type: TreasureType, | |
value: u32, | |
rarity: u8, // 1-10 scale, 10 being the rarest | |
} | |
struct DragonHoard { | |
dragon_name: String, | |
treasures: Vec<Treasure>, | |
} | |
impl DragonHoard { | |
// Create a new empty hoard for a dragon | |
fn new(name: &str) -> Self { | |
// TODO: Initialize a new DragonHoard with the given name | |
unimplemented!() | |
} | |
// Add a treasure to the dragon's hoard | |
fn add_treasure(&mut self, treasure: Treasure) { | |
// TODO: Add the treasure to the hoard | |
unimplemented!() | |
} | |
// Calculate the total value of the hoard | |
fn total_value(&self) -> u32 { | |
// TODO: Return the sum of all treasure values | |
unimplemented!() | |
} | |
// Get the top N rarest treasures in the hoard | |
fn rarest_treasures(&self, n: usize) -> Vec<&Treasure> { | |
// TODO: Return the top N treasures sorted by rarity (highest first) | |
// If there are fewer than N treasures, return all of them | |
unimplemented!() | |
} | |
// Calculate the value of treasures by type | |
fn value_by_type(&self) -> HashMap<TreasureType, u32> { | |
// TODO: Return a HashMap with the total value for each treasure type | |
unimplemented!() | |
} | |
// Trade a treasure with another dragon (remove from this hoard, add to other) | |
fn trade_treasure(&mut self, treasure_name: &str, other_dragon: &mut DragonHoard) -> Result<(), &'static str> { | |
// TODO: Find the treasure by name, remove it from this hoard and add to the other | |
// Return an error if the treasure doesn't exist | |
unimplemented!() | |
} | |
} | |
// For testing: | |
fn main() { | |
// Create two dragon hoards | |
let mut smaug = DragonHoard::new("Smaug"); | |
let mut falkor = DragonHoard::new("Falkor"); | |
// Add treasures to Smaug's hoard | |
smaug.add_treasure(Treasure { | |
name: "Arkenstone".to_string(), | |
treasure_type: TreasureType::MagicalArtifact, | |
value: 1000, | |
rarity: 10, | |
}); | |
smaug.add_treasure(Treasure { | |
name: "Gold Coin".to_string(), | |
treasure_type: TreasureType::GoldCoin, | |
value: 1, | |
rarity: 1, | |
}); | |
smaug.add_treasure(Treasure { | |
name: "Ruby".to_string(), | |
treasure_type: TreasureType::Gem, | |
value: 200, | |
rarity: 6, | |
}); | |
// Test total value | |
println!("{}'s hoard is worth {} gold", smaug.dragon_name, smaug.total_value()); | |
// Test rarest treasures | |
let rarest = smaug.rarest_treasures(2); | |
println!("\nRarest treasures:"); | |
for treasure in rarest { | |
println!("- {} (rarity: {})", treasure.name, treasure.rarity); | |
} | |
// Test value by type | |
let type_values = smaug.value_by_type(); | |
println!("\nValue by type:"); | |
for (_, value) in &type_values { | |
// You'll need to implement a proper display for TreasureType | |
println!("- Type: {}", value); | |
} | |
// Test trading | |
println!("\nTrading treasure..."); | |
match smaug.trade_treasure("Ruby", &mut falkor) { | |
Ok(()) => { | |
println!("Trade successful!"); | |
println!("{}'s hoard is now worth {}", smaug.dragon_name, smaug.total_value()); | |
println!("{}'s hoard is now worth {}", falkor.dragon_name, falkor.total_value()); | |
}, | |
Err(e) => println!("Trade failed: {}", e), | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment