Bloomberg LP Interview Question for Software Developers


Country: United States
Interview Type: Phone Interview




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

Implemented in C, a hashmap for <string, string> map. Used a chained hashing method

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define N 20


typedef struct X{
    char* key;
    char* value;
    struct X* next;
}Node;

Node* hasharray[N] = {NULL};

int hashcalc(char* key){
    int val = 0;
    int count = 0;
    while(*key != 0){
        val = val*(*key) + *key + count;
        count++;
        key++;
    }

    if(val < 0) val *= -1;

    val = val%N;
    return val;
}

void put(char* key, char* value){

    int hashval = hashcalc(key);
    Node* nd = hasharray[hashval];
    while(nd != NULL){
        if(strcmp(nd->key, key) == 0){
            nd->value = value;
            return;
        }
    }
    nd = (Node*) malloc(sizeof(Node));
    nd->key = key;
    nd->value = value;
    nd->next = hasharray[hashval];
    hasharray[hashval] = nd;

}

char* get(char* key){

    int hashval = hashcalc(key);
    Node* nd = hasharray[hashval];
    while(nd != NULL){
       if(strcmp(key, nd->key) == 0){
           return nd->value;
       } 
    }

}

void main(){
    printf("%s\n", __func__);
    put("alpha","bravo");
    put("zero", "0");
    put("alpha", "charlie");
    printf("%s\n", get("alpha"));
    printf("%s\n", get("zero"));

}

- blurred July 22, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

In the above code, shouldn't a new value for the same key be added to the linkedlist associated for that Hash(key). This code basically replaces the value of that hash_key to the new value.

- anil.kumar.848 October 29, 2015 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

In the above code, shouldn't a new value for the same key be added to the linkedlist associated for that Hash(key). This code basically replaces the value of that hash_key to the new value.

- anil.kumar.848 October 29, 2015 | Flag Reply


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