Student.be logo

NL

News

>

Afgestudeerd, wat nu?

>

How happy numbers can spice up a programmer’s job interview

How happy numbers can spice up a programmer’s job interview

door OMP

04/05/2022

3 mins

Deel dit artikel

How happy numbers can spice up a programmer’s job interview  banner image

Imagine you’re in a spaceship being hunted down by co-workers who’ve been taken over by an evil force. The only way to open the lock to the safe area of the ship is by finding a set of happy primes. This is exactly what happened to the main character in the sci-fi TV series Dr. Who.

We all know what a prime number is, and there are numerous algorithms to determine whether a number is a prime, but what is a happy number? Well, the term “happy number” comes from the world of recreational mathematics and it even has its own Wikipedia page. The definition is actually very simple:

  • Start with a number N
  • Take all the digits from N, square them and add them together
  • Then repeat the operation on the result

If repeated execution of this operation ends in the number 1, the number N is called a happy number. If it results in an endless loop, the number N is called an unhappy, or sad number.

Wrong for two reasons

At a job interview, you might get a question like: “Write an algorithm to determine whether a number is a happy number. It should return true if it’s happy, false if it’s unhappy”. Let’s try to solve this problem.

Getting the digits from a number seems easy, and you might be tempted to write it like this:

char buffer[3];

sprintf (buffer, “%d”, number);

size_t sum = buffer[0]*buffer[0] + buffer[1]*buffer[1];

This code is deceptively simple but it’s wrong for two reasons:

  • It incorrectly assumes that the numeric value of a character is equal to the numeric value of the digit. It’s not. The character ‘0’ has ASCII value 0x30, which clearly isn’t zero;
  • It assumes that the number consists of only 2 digits. Even when the original question would have been ‘start with a number between 10 and 99’, there is no guarantee that all intermediate values remain smaller than 100.

Look mom

So, lesson #1 would be: when dealing with numeric information, you should use numeric algorithms. Getting the digits from a number is actually very easy: just use modulo-10 to get one digit, perform an integer divide-by-10, and repeat. While looping, take the square of the digits, like this:

auto sum = size_t{0};

while (number)

{

auto remainder = number%10;

sum += remainder * remainder;

number /= 10;

Bekijk ook onze lijst van startersjobs!

}

Look mom, no sprintf function needed.

Check using a vector

The next step is to repeat this operation until we reach 1. Let’s wrap the calculation above in a function called calculate and write the final algorithm:

auto isHappy(int number)

{

while (number!=1)

number = calculate(number);

return true;

}

Wait, and exactly when do we return false? What happens if the number is not a happy number? Right, the function will never terminate. What’s missing is a check on whether we already encountered the number before.

The most obvious way to check this is to keep all values in a simple std::vector. In every iteration, check whether the value is already in the vector, and if it isn’t, add it to the vector and continue.

Extending the question

The interviewer might ask you for alternative ways to check for recurring numbers. Would an std::set be more efficient? Should we use an std::bitset to check whether we already encountered a number? What would be the fastest possible implementation?

Try to extend the original question to show the interviewer that you are really thinking outside the box, not bound by what you learned from textbooks. For example, propose to write an algorithm that returns all happy numbers between 1 and 1 million. It could be a good opportunity to show your knowledge of multi-threading too.

No spaceships, still a lot of fun

Okay, that was fun, but will we ever need to solve this kind of problem in real life? Probably not. But lots of other problems will come your way and solving them is important when you work in an innovative software company like OMP. We don’t build spaceships and our co-workers haven’t been taken over by an evil force, but OMP is always looking for people with a spark of creativity.

Ready to start thinking outside of the box? Discover our software engineering jobs below this article and apply today!

Lees andere artikelen per onderwerp👇

Welk als stagiair, student of young professional bij OMP
OMP

OMP

Supply Chain en logistiek

Wij zijn OMP, een game changer in supply chain planning oplossingen. We helpen ’s werelds grootste bedrijven bij het optimaliseren van hun supply chains met behulp van onze slimme software en services.   Ben jij klaar om deel uit te

Altijd als eerste op de hoogte van de laatste starterjobs! 📩

Wat vond je van deze post?

thumbs-up
light-bulb
laugh
fire
rolling-eyes

Verdiep je verder

Meer artikelen zoals deze

sector image
Afgestudeerd, wat nu?
De BDO Accountancy Challenge 2026

1 min. lezen

sector image
Afgestudeerd, wat nu?
Net zoals Ender een tikje delulu? Word accountant!

2 min. lezen

sector image
Afgestudeerd, wat nu?
Ontdek onze Young Talent Programs via een exclusieve webinar!

1 min. lezen

sector image
Afgestudeerd, wat nu?
Het verhaal van Ashley: Van criminologie naar cybersecurity bij KBC

3 min. lezen

sector image
Afgestudeerd, wat nu?
Top Employer 2026: deze werkgevers zijn een sterke start voor jouw eerste job

2 min. lezen

sector image
Afgestudeerd, wat nu?
Zo ziet jouw eerste werkdag bij Jan De Nul eruit

1 min. lezen

Alles op een rijtje gezet voor jou

Studentenjob, stage, startersjob of kot — vind het in je buurt en ga ervoor!

lightning

Studentenjobs

  • Aalst
  • Antwerpen
  • Brugge
  • Brussel
  • Gent
  • Hasselt
  • Kortrijk
  • Leuven
  • Mechelen
  • Oostende

Studentenjobs in alle steden

graduate

Stages

  • Aalst
  • Antwerpen
  • Brugge
  • Brussel
  • Gent
  • Hasselt
  • Kortrijk
  • Leuven
  • Mechelen
  • Oostende

Stages in alle steden

first-job

Jobs

  • Aalst
  • Antwerpen
  • Brugge
  • Brussel
  • Gent
  • Hasselt
  • Kortrijk
  • Leuven
  • Mechelen
  • Oostende

Jobs in alle steden

trainee

Koten

  • Aalst
  • Antwerpen
  • Brugge
  • Brussel
  • Gent
  • Hasselt
  • Kortrijk
  • Leuven
  • Mechelen
  • Oostende

Koten in België

Student.be logo
tiktoklinkedininstagramfacebookyoutube

Studenten

& young professionals

Creëer jouw job alertZoek een studentenjobZoek een stageZoek een startersjobOntdek alle bedrijvenStudiekeuze & studierichtingenZoek een kotStudentenkortingen

Gratis stuff

Gids studentenlonenGids starterslonen

Werkgevers

Ontdek onze aanpakFormules & tarievenVoor start-upsPlaats een job onlineTips voor werkgeversContacteer ons

Scholen & universiteiten

Samen studenten ondersteunenContacteer ons

Jobbeurzen en events

Jobday Sciences ULB-VUBJobfair Engineers ULBLocal' Job Fair ALIFUCaMSolvay Startup WorldBusiness school fair

Koteigenaren

Plaats een kot onlineInfo & voorwaarden

Student.be

Wie zijn we?SnakLab: onze video brandPodcastsStudent in de persFAQAPI documentatieContacteer ons

Werkgevers

Ontdek onze aanpakFormules & tarievenVoor start-upsPlaats een job onlineTips voor werkgeversContacteer ons

Scholen & universiteiten

Samen studenten ondersteunenContacteer ons

Events

Jobday Sciences ULB-VUBJobfair Engineers ULBLocal' Job Fair ALIFUCaMSolvay Startup WorldBusiness school fair

Studenten

& young professionals

Creëer jouw job alertZoek een studentenjobZoek een stageZoek een startersjobOntdek alle bedrijvenStudiekeuze & studierichtingenZoek een kotStudentenkortingen

Koteigenaren

Plaats een kot onlineInfo & voorwaarden

Gratis stuff

Gids studentenlonenGids starterslonen

Student.be

Wie zijn we?SnakLab: onze video brandPodcastsStudent in de persFAQAPI documentatieContacteer ons

© 2026 Student.be. All rights reserved - Cookies


Algemene voorwaarden

Privacyverklaring

Badge
FR EN NL