Checking Anagrams in C++ for Embedded Systems Code Interview

  algorithm, anagram, c++

How should I write an anagram checker in C++ for an embedded systems engineer code interview?

In a recent C++ code interview, I was asked to write code that checks if two strings were anagrams of each other.

Two words are anagrams if their letters are the same, just rearranged.

Embedded systems engineers have certain considerations (memory model, and maybe others?) that are the types of things interviewers want to feel confident that interviewees understand. I answered the question, but was provided limited context for what knowledge outcomes the interviewer was interested in. (I asked, and the response was essentially "show us what you know").

From an embedded systems perspective, what might this interview question be used to explore? (Also note that interviewer was primarily experienced in embedded C, in case this provides you any relevant context).

The solution I provided is below. Alternative solutions without the standard library are acceptable, especially if they illustrate limitations or benefits of C++.

#include <iostream>
#include <string>
#include <algorithm>

std::string ana = "cinema";
std::string gram = "iceman";

int main ()
{
    std::sort(ana.begin(), ana.end());       //if this
    std::sort(gram.begin(), gram.end());     //equals this

    std::cout << (ana == gram) << std::endl; //we're good
}

Source: Windows Questions C++

LEAVE A COMMENT