Was contacted by recruiter through linkedin profile. Had a initial phone interview with collabedit. Was not offered an onsite interview.
Was asked to write a program that would take a filename and a search pattern as arguments and return if the pattern matches the filename. Wildcards * and ? can be used in the pattern.
eg:
1. filename: file.txt pattern: f*txt result pattern matches
2. filename:file.txt pattern:f?le* result: pattern matches
Answer:
bool matchFilename(const string& flname, const string& patter)
{
for (int i=0,j=0;i<flname.length(),j<patter.length();i++,j++)
{
if (patter.at(j)=='*')
{
if (j+1<patter.length())
{
i = flname.find(patter.at(j+1),i+1);
i--;
}
}
else if (patter.at(j)=='?')
{/*do nothing*/}
else
{
if (flname.at(i)!=patter.at(j))
return false;
}
}
return true;
}