Igate Interview Question for Technical Architects


Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
0
of 0 vote

Yes, you can. But this is going to be pretty useless cause the key comparison is going to be done for pointers themselves, not for the memory content they point to, which is what is presumed the char* is meant to be used for. Consider the less predicate acting upon two pointers: char *p1 < char*p2. Is this what we want ? Definitely not.

- ashot madatyan July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

There are plenty of times you'd want to use char* in a map. For instance, if you're associating the identity and not the value of c-style strings (as a key) with some other value.

- eugene.yarovoi July 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Evgeny
I am not sure that I quite understood the idea of using the " char* " as a key in a map for associating the identity. Could you please elaborate on that?

- ashot madatyan July 24, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I mean, what if you didn't want to pass a string by value, but you wanted to just pass it by identity (two strings are equal if they are in the same place in memory). If these are C-style strings and if we wish to use them as keys in maps, char* would make sense. Not a common case, but plausible.

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Why not char*? Char& is another story though...

- eugene.yarovoi July 24, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

No story for char& because it will evolved as "char" only which is standard datatype.

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Anon: I don't understand what you're saying.

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

char * is a address to char. So, problem is map will store char * which will be numerically order (which is not expected).

However, char& is reference to char and compiler can treat it as char. So, There will not have any problem to store a char (OR char &) as a key in map.

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

That's not what happens when you declare the template type to be char&, as in

std::map<char&, valueType> foo;

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it will just make a reference to char. So, map will be maintained correctly either key is "char" (copy of a char) or "char &" (reference to a char).

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

@Last Anonymous: No, you can't use references in STL containers. See this stack overflow topic: stackoverflow .com/questions/2934021/stl-map-containing-references-does-not-compile

- eugene.yarovoi November 08, 2012 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

functors is a solution for using any non standard data types.check below link

stackoverflow.com/questions/356950/c-functors-and-their-uses

- Anonymous July 25, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

While an interesting read, it's fairly irrelevant to this specific discussion.

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

link was just passed looking at the header. read this, i hope you will understand that functors is the solution.

stackoverflow.com/questions/4157687/using-char-as-a-key-in-stdmap

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I'm sorry to tell you that I don't understand why you think functors are relevant here. While functors are a very useful construct that I myself have happily used in the past, I don't see how they answer the question here. The original problem statement just asks whether it's possible to use char* as the key type in a map. And the answer is yes, it is. When someone answered that it's useless, I felt compelled to tell them that there are sometimes occasions to do it. But what do functors have to do with this discussion?

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Okey....... May be question is incomplete because no one will just asked whether "char *" can be used as a map key.

The answer to actual question. Yes, you can use char * as a key. and answer to "how?" is functors and that is explained in earlier link.

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You can use char* as a key without using functors. That's why I'm having a lot of trouble seeing how functors are relevant here.

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Right !!! You can use char * without any problem. But think, what is the use of it and how it going to work? Take an example. you have defined map like,

std::map <char *, valuetype> myMap;

Now, You have added, addresses of these string "abc", "xyz", "ttt","oooo" with there respective value (of type valueType) into map. First thing, how those get inserted in map ? Now you want to search "ttt" in a map. will it work as expected?

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Lucky that I don't expect searching for "ttt" to work then. I don't expect value-based searches to work. In this case, I would only expect searching for the pointer to work. This is still quite useful if I'm getting these pointers from another data structure that's supposed to work with this one or something like that.

- eugene.yarovoi July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

so it means, you need maintain another data structure which will search your value and return you address to be searched into map. And that increases complexity. functors provides you easy solution.

- Anonymous July 25, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You don't need to maintain anything. I'm saying, you might use a map with a char* if you were maintaining such a thing. There are times when it's useful. It's useful for the same reason why using pointers instead of passing everything by value is sometimes useful.

And what in the world do functors have to do with anything? You still haven't explained that. The problem statement is to make a map with char*, not to do anything else.

- eugene.yarovoi July 25, 2012 | Flag


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More