This is an attempted solution of a problem on codefights: https://codefights.com/interview-practice/task/FwAR7koSB3uYYsqDp
My BFS function is not returning the correct character despite me printing right before the return and seeing the correct character in the console. It seems the chracter is being mutated for some reason. When I change the function signature to have a std::string return value, the program crashes. I have no clue what I'm doing wrong. Is it possibly due to lack of freeing pointers or something?
typedef struct proTree{
char value;
proTree* left;
proTree* right;
} proTree;
char BFS(std::vector<proTree*> vec, int currLevel, int level, int pos){
if (currLevel == level){
if (vec[pos-1]->value == 'E'){
return 'E';
} else {
return 'D';
}
}
std::vector<proTree*> newVec;
for (int i=0; i<vec.size(); i++){
newVec.push_back(vec[i]->left);
newVec.push_back(vec[i]->right);
}
BFS(newVec, currLevel+1, level, pos);
}
void createTree(proTree* root, int currLevel, int level){
if (currLevel == level) return;
proTree* eTree = new proTree();
eTree->value = 'E';
proTree* dTree = new proTree();
dTree->value = 'D';
if (root->value=='E'){
root->left = eTree;
root->right = dTree;
} else {
root->left = dTree;
root->right = eTree;
}
createTree(eTree, currLevel+1, level);
createTree(dTree, currLevel+1, level);
}
std::string findProfession(int level, int pos) {
proTree* eTree = new proTree();
eTree->value = 'E';
createTree(eTree, 0, level);
std::vector<proTree*> vec = {eTree};
char result = BFS(vec, 0, level, pos);
if (result == 'E'){
return "Engineer";
} else {
return "Doctor";
}
}
BFS does not return anything (actually returned value is undefined) because last function line is missing return and the value of recursive function invocation is lost. It should be:
return BFS(newVec, currLevel+1, level, pos);
You should pay attention to compilation warnings. In this case compiler should've definitely complained about "missing return in function returning non-void" or something similar.
Related
I have been going through the debugger but can't seem to pinpoint exactly what is going wrong. I have come to my own conclusion i must be missing a nullptr check somewhere or something. If anyone can provide some help it would be greatly appreciated.
error message from debugger
error msg
which looks like makes the program crash on this line:
if (node->children_[index] == nullptr) {
search function
Node* search(const string& word, Node* node, int index) const {
Node* temp;
//same as recurssive lookup just difference is returns node weather terminal or not
if (index < word.length()) {
index = node->getIndex(word[index]);
if (node->children_[index] == nullptr) {
return nullptr;
}
else {
temp = search(word, node->children_[index], index++);
}
}
return temp; // this would give you ending node of partialWord
}
Node struct for reference
struct Node {
bool isTerminal_;
char ch_;
Node* children_[26];
Node(char c = '\0') {
isTerminal_ = false;
ch_ = c;
for (int i = 0; i < 26; i++) {
children_[i] = nullptr;
}
}
//given lower case alphabetic charachters ch, returns
//the associated index 'a' --> 0, 'b' --> 1...'z' --> 25
int getIndex(char ch) {
return ch - 'a';
}
};
Node* root_;
int suggest(const string& partialWord, string suggestions[]) const {
Node* temp;
temp = search(partialWord, root_, 0);
int count = 0;
suggest(partialWord, temp, suggestions, count);
return count;
}
Might be a very simple thing. Without digging I am not sure about the rank of the -> operator versus the == operator. I would take a second and try putting parenthesis around the "node->children_[index] == nullptr" part like this:
(node->children_[index]) == nullptr
just to make sure that the logic runs like you seem to intend.
Dr t
I believe the root cause is that you're using index for two distinct purposes: as an index into the word you're looking for, and as an index into the node's children.
When you get to the recursion, index has changed meaning, and it's all downhill from there.
You're also passing index++ to the recursion, but the value of index++ is the value it had before the increment.
You should pass index + 1.
[An issue in a different program would be that the order of evaluation of function parameters is unspecified, and you should never both modify a variable and use it in the same parameter list. (I would go so far as to say that you should never modify anything in a parameter list, but many disagree.)
But you shouldn't use the same variable here at all, so...]
I would personally restructure the code a little, something like this:
Node* search(const string& word, Node* node, int index) const {
// Return immediately on failure.
if (index >= word.length())
{
return nullptr;
}
int child_index = node->getIndex(word[index]);
// The two interesting cases: we either have this child or we don't.
if (node->children_[child_index] == nullptr) {
return nullptr;
}
else {
return search(word, node->children_[child_index], index + 1);
}
}
(Side note: returning a pointer to a non-const internal Node from a const function is questionable.)
Struct Node {
Node *N[SIZE];
int value;
};
struct Trie {
Node *root;
Node* findNode(Key *key) {
Node *C = &root;
char u;
while (1) {
u = key->next();
if (u < 0) return C;
// if (C->N[0] == C->N[0]); // this line will speed up execution significantly
C = C->N[u];
if (C == 0) return 0;
}
}
void addNode(Key *key, int value){...};
};
In this implementation of Prefix Tree (aka Trie) I found out that 90% of findNode() execution time is taken by a single operation C=C->N[u];
In my attempt to speed up this code, I randomly added the line that is commented in the snipped above, and code became 30% faster! Why is that?
UPDATE
Here is complete program.
#include "stdio.h"
#include "sys/time.h"
long time1000() {
timeval val;
gettimeofday(&val, 0);
val.tv_sec &= 0xffff;
return val.tv_sec * 1000 + val.tv_usec / 1000;
}
struct BitScanner {
void *p;
int count, pos;
BitScanner (void *p, int count) {
this->p = p;
this->count = count;
pos = 0;
}
int next() {
int bpos = pos >> 1;
if (bpos >= count) return -1;
unsigned char b = ((unsigned char*)p)[bpos];
if (pos++ & 1) return (b >>= 4);
return b & 0xf;
}
};
struct Node {
Node *N[16];
__int64_t value;
Node() : N(), value(-1) { }
};
struct Trie16 {
Node root;
bool add(void *key, int count, __int64_t value) {
Node *C = &root;
BitScanner B(key, count);
while (true) {
int u = B.next();
if (u < 0) {
if (C->value == -1) {
C->value = value;
return true; // value added
}
C->value = value;
return false; // value replaced
}
Node *Q = C->N[u];
if (Q) {
C = Q;
} else {
C = C->N[u] = new Node;
}
}
}
Node* findNode(void *key, int count) {
Node *C = &root;
BitScanner B(key, count);
while (true) {
char u = B.next();
if (u < 0) return C;
// if (C->N[0] == C->N[1]);
C = C->N[0+u];
if (C == 0) return 0;
}
}
};
int main() {
int T = time1000();
Trie16 trie;
__int64_t STEPS = 100000, STEP = 500000000, key;
key = 0;
for (int i = 0; i < STEPS; i++) {
key += STEP;
bool ok = trie.add(&key, 8, key+222);
}
printf("insert time:%i\n",time1000() - T); T = time1000();
int err = 0;
key = 0;
for (int i = 0; i < STEPS; i++) {
key += STEP;
Node *N = trie.findNode(&key, 8);
if (N==0 || N->value != key+222) err++;
}
printf("find time:%i\n",time1000() - T); T = time1000();
printf("errors:%i\n", err);
}
This is largely a guess but from what I read about CPU data prefetcher it would only prefetch if it sees multiple access to the same memory location and that access matches prefetch triggers, for example looks like scanning. In your case if there is only single access to C->N the prefetcher would not be interested, however if there are multiple and it can predict that the later access is further into the same bit of memory that can make it to prefetch more than one cache line.
If the above was happening then C->N[u] would not have to wait for memory to arrive from RAM therefore would be faster.
It looks like what you are doing is preventing processor stalls by delaying the execution of code until the data is available locally.
Doing it this way is very error prone unlikely to continue working consistently. The better way is to get the compiler to do this. By default most compilers generate code for a generic processor family. BUT if you look at the available flags you can usually find flags for specifying your specific processor so it can generate more specific code (like pre-fetches and stall code).
See: GCC: how is march different from mtune? the second answer goes into some detail: https://stackoverflow.com/a/23267520/14065
Since each write operation is costly than the read.
Here If you see that,
C = C->N[u]; it means CPU is executing write in each iteration for the variable C.
But when you perform if (C->N[0] == C->N[1]) dummy++; write on dummy is executed only if C->N[0] == C->N[1]. So you have save many write instructions of CPU by using if condition.
in this BST,the searchbst function is searching perfectly but this function is returning 0 always. It is not giving 5 or 8 as i have programmed it to do so what is the error in code because of which this problem is there
#include<iostream>
using namespace std;
struct bstnode{
bstnode *lchild;
int data;
bstnode *rchild;
};
void creatbst(bstnode *&T,int k){
if(T=='\0'){
T=new(bstnode);
T->data=k;
T->lchild='\0';
T->rchild='\0';
}
else if(k<T->data){
creatbst(T->lchild,k);
}
else if(k>T->data){
creatbst(T->rchild,k);
}
}
int searchbst(bstnode *T,int k){
if(T=='\0')
return 5;
else{
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
}
}
int main(){
bstnode *T;
T='\0';
creatbst(T,36);
creatbst(T,20);
creatbst(T,75);
creatbst(T,42);
creatbst(T,8);
creatbst(T,31);
creatbst(T,25);
creatbst(T,3);
creatbst(T,80);
cout<<endl<<"searching for ";
cout<<searchbst(T,3);
cout<<endl<<"searching for ";
cout<<searchbst(T,1);
return 0;
}
You're not using the return value of the recursive calls.
Replace:
if(k<T->data)
searchbst(T->lchild,k);
else if(k>T->data)
searchbst(T->rchild,k);
else
return 8;
with:
if(k < T->data)
return searchbst(T->lchild, k);
else if(k > T->data)
return searchbst(T->rchild, k);
else
return 8;
Your code most likely has undefined behavior: If you don't follow the conditions leading to the return 5; or the return 8; statements, you call searchbst(), ignore its result, and eventually you fall of the function. You probably meant to return the result of searchbst():
return searchbst(T->rchild, k);
BTW, you have a funny way of writing the null pointer constant: although '\0' works, the conventional way is to use 0 or nullptr (the latter is the C++11 way).
The following code instead of returning a pointer back to an audioResource it returns
something else which is invalid, i've gone through with a debugger and the problem is with this line
return *list_it;
Here is my function:
AudioResource* AudioManager::getResource(const unsigned int ID)
{
std::list<AudioResource*>::iterator list_it;
for(list_it = m_resources.begin(); list_it!= m_resources.end(); list_it++)
{
if((*list_it)->getID()==ID)
{
std::cout<<*(*list_it)->getFileName();
return *list_it;
}
}
return nullptr;
}
O and I have tried putting it as (*list_it) but i got the same results =s
How it is populated...
Resource* AudioManager::addResource(const unsigned int ID,
const std::string fileName, const unsigned int scope,
const std::string type)
{
AudioResource* temp;
if(type == "AUDIO_TYPE_SAMPLE")
{
temp = new AudioResource(ID,fileName,scope,
RESOURCE_AUDIO,AUDIO_TYPE_SAMPLE);
m_resources.push_back(temp);
}
else if(type == "AUDIO_TYPE_STREAM")
{
temp = new AudioResource(ID,fileName,scope,
RESOURCE_AUDIO,AUDIO_TYPE_STREAM);
m_resources.push_back(temp);
}
return temp;
}
call to get resource
cout<<AudioManager::getInstance()->getResource(IDnum)->getFileName();
If type is neither of the two values an uninitialized pointer is added to m_resources:
AudioResource* temp;
if(type == "AUDIO_TYPE_SAMPLE")
{
temp = new AudioResource(ID,fileName,scope,RESOURCE_AUDIO,AUDIO_TYPE_SAMPLE);
}
else if(type == "AUDIO_TYPE_STREAM")
{
temp = new AudioResource(ID,fileName,scope,RESOURCE_AUDIO,AUDIO_TYPE_STREAM);
}
m_resources.push_back(temp);
Initialize temp to NULL and only add to m_resources if temp != NULL.
Also, the function returns the same uninitialized pointer.
You return nullptr in case the ID doesn't exist, but you never check against it at the call site, which will give you a null pointer access if the ID doesn't exist and which will likely create problems.
AudioManager::getInstance()->getResource(IDnum)->getFileName();
Change that to
AudioResource* res = AudioManager::getInstance()->getResource(IDnum);
if(res)
std::cout << res->getFileName();
struct Ternary {
char current;
bool wordend;
Ternary* left;
Ternary* mid;
Ternary* right;
Ternary(char c='#',Ternary* l=NULL, Ternary* m=NULL, Ternary* r=NULL,bool end=false)
{
wordend=end;
current=c;
left=l;
mid=m;
right=r;
}
};
void add(Ternary* t, string s, int i) {
if (t == NULL) {
Ternary* temp = new Ternary(s[i],NULL,NULL,NULL,false);
t=temp;
}
if (s[i] < t->current) {
add(t->left,s,i);
}
else if (s[i] > t->current) {
add(t->right,s,i);
}
else
{
if ( i + 1 == s.length()) {
t->wordend = true;
}
else
{
add(t->mid,s,i+1);
}
}
}
When I add sequence of words using add() the string are getting printed inside
if(t==NULL) segment but tree isn't getting formed i.e nodes are not getting linked.
t=temp;
This line has no effect outside of the add() function. The caller's pointer is not updated.
You could change your function to return a Ternary* (return t in this case at the end of it), and change the call sites to:
Ternary *tree = 0;
tree = add(tree, "hello", 1);
tree = add(tree, "bye", 1);
...
Just a little trick will do:
Replace:
void add(Ternary* t, string s, int i)
With:
void add(Ternary*& t, string s, int i)
That's cleaner than passing and then reading output like this:
tree = add(tree, "bye", 1);
When in C++, make use of their references :) In C you would change the function signature to:
void add(Ternary** t, string s, int i)
and remember to correct t in relevant places.
Well, C++ is clearly cleaner :)