Project · Interview practice
Stock decision review harness
Twelve independent boundary and contract checks around pure checked stock decisions, without external storage.
All C# source files below are loaded together in the playground. Files written by a run live in its virtual directory.
Interview.csproj
<Project Sdk="Microsoft.NET.Sdk"><PropertyGroup><OutputType>Exe</OutputType><TargetFramework>net9.0</TargetFramework><LangVersion>13.0</LangVersion><ImplicitUsings>enable</ImplicitUsings><Nullable>enable</Nullable></PropertyGroup></Project>
Program.cs
Console.WriteLine(ReviewChecks.Run()+" stock review checks passed");
Domain/StockRules.cs
public sealed record StockInput(int Available,int Quantity,long PriceCents);
public sealed record StockDecision(string Status,long Total);
public static class StockRules {
public static StockDecision Decide(StockInput value) {
if(value.Available<0 || value.Quantity<=0 || value.PriceCents<0) return new("invalid",0);
if(value.Quantity>value.Available) return new("insufficient",0);
return new("accepted",checked(value.Quantity*value.PriceCents));
}
}
Checks/ReviewChecks.cs
public static class ReviewChecks {
public static int Run() {
int count=0; void Check(bool ok,string name){if(!ok)throw new InvalidOperationException(name);count++;}
Check(StockRules.Decide(new(4,2,150))==new StockDecision("accepted",300),"ordinary");
Check(StockRules.Decide(new(4,4,150)).Total==600,"exact stock");
Check(StockRules.Decide(new(4,5,150)).Status=="insufficient","over stock");
Check(StockRules.Decide(new(4,0,150)).Status=="invalid","zero");
Check(StockRules.Decide(new(4,-1,150)).Status=="invalid","negative quantity");
Check(StockRules.Decide(new(-1,1,150)).Status=="invalid","negative stock");
Check(StockRules.Decide(new(4,1,-1)).Status=="invalid","negative price");
Check(StockRules.Decide(new(1,1,0))==new StockDecision("accepted",0),"free valid");
Check(StockRules.Decide(new(2,2,int.MaxValue)).Total==4294967294L,"wide multiply");
bool overflow=false;try{StockRules.Decide(new(2,2,long.MaxValue));}catch(OverflowException){overflow=true;} Check(overflow,"overflow");
var input=new StockInput(3,2,10);_ = StockRules.Decide(input);Check(input==new StockInput(3,2,10),"input unchanged");
Check(StockRules.Decide(new(-1,0,-1)).Status=="invalid","precedence");return count;
}
}