Aqemia interview question

hard programming problem in 30 mins