├── BST.cpp ├── MultipleChoiceTest.cpp ├── README.md ├── palindrome.cpp ├── path.cpp ├── sortedSearch.cpp ├── trainComposition.cpp ├── twoSum.cpp └── userInput.cpp /BST.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // BST.cpp 3 | // BST 4 | // 5 | // Created by Shawn O'Grady on 12/1/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | /* 9 | This code is a practice C++ interview question from testdome.com 10 | https://www.testdome.com/for-developers/solve-question/12977 11 | 12 | Problem statement: "Write a function that checks if a given binary search tree contains a given value" 13 | */ 14 | 15 | #include 16 | #include 17 | #include 18 | 19 | class Node 20 | { 21 | public: 22 | Node(int value, Node* left, Node* right) 23 | { 24 | this->value = value; 25 | this->left = left; 26 | this->right = right; 27 | } 28 | 29 | int getValue() const 30 | { 31 | return value; 32 | } 33 | 34 | Node* getLeft() const 35 | { 36 | return left; 37 | } 38 | 39 | Node* getRight() const 40 | { 41 | return right; 42 | } 43 | 44 | private: 45 | int value; 46 | Node* left; 47 | Node* right; 48 | }; 49 | class BinarySearchTree 50 | { 51 | public: 52 | static bool contains(const Node& root, int value) 53 | { 54 | 55 | if(root.getValue()==value){ 56 | return true; 57 | } 58 | else if(root.getValue() 23 | #include 24 | #include 25 | 26 | class MultipleChoiceTest 27 | { 28 | public: 29 | MultipleChoiceTest(int questionsCount) 30 | { 31 | this->questionsCount = questionsCount; 32 | answers = new int[questionsCount]; 33 | for (int i = 0; i < questionsCount; i++) 34 | { 35 | answers[i] = -1; 36 | } 37 | 38 | } 39 | 40 | void setAnswer(int questionIndex, int answer) 41 | { 42 | answers[questionIndex] = answer; 43 | } 44 | 45 | int getAnswer(int questionIndex) const 46 | { 47 | return answers[questionIndex]; 48 | } 49 | virtual ~MultipleChoiceTest(){ 50 | //destructor, deallocate memory 51 | delete[] answers; 52 | } 53 | 54 | protected: 55 | int questionsCount; 56 | 57 | private: 58 | int* answers; 59 | }; 60 | 61 | class TimedMultipleChoiceTest : public MultipleChoiceTest 62 | { 63 | public: 64 | TimedMultipleChoiceTest(int questionsCount) 65 | : MultipleChoiceTest(questionsCount) 66 | { 67 | times = new int[questionsCount]; 68 | for (int i = 0; i < questionsCount; i++) 69 | { 70 | times[i] = 0; 71 | } 72 | } 73 | 74 | void setTime(int questionIndex, int time) 75 | { 76 | times[questionIndex] = time; 77 | } 78 | 79 | int getTime(int questionIndex) const 80 | { 81 | return times[questionIndex]; 82 | } 83 | ~TimedMultipleChoiceTest() 84 | { 85 | //destructor, deallocate memory 86 | delete[] times; 87 | } 88 | 89 | private: 90 | int* times; 91 | }; 92 | /* 93 | #ifndef RunTests 94 | void executeTest() 95 | { 96 | MultipleChoiceTest test(5); 97 | for (int i = 0; i < 5; i++) 98 | { 99 | test.setAnswer(i, i); 100 | } 101 | 102 | for (int i = 0; i < 5; i++) 103 | { 104 | std::cout << "Question " << i + 1 << ", correct answer: " << test.getAnswer(i) << "\n"; 105 | } 106 | } 107 | 108 | int main() 109 | { 110 | for (int i = 0; i < 3; i++) 111 | { 112 | std::cout << "Test: " << i + 1 << "\n"; 113 | executeTest(); 114 | } 115 | } 116 | #endif 117 | */ 118 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TestdomeCpp 2 | 3 | Since I have been out of school for a few months, I figured that I could use some extra practice with C++ to make sure I maintain my level of profficiency. 4 | 5 | In this repository I am answering some sample C++ interview questions supplied by testdome.com [(link to full set of questions)](https://www.testdome.com/d/cpp-interview-questions/7). 6 | 7 | Each file in this repository corresponds to one of the questions from the link above. While I assume the filenames will be descriptive enough to know which files correspond to which questions, I will also provide a direct link to the question I am answering in the header comments of the file. 8 | 9 | All files were written and tested in Xcode version 8.3.3 10 | -------------------------------------------------------------------------------- /palindrome.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // palindrome.cpp 3 | // Palindrome 4 | // 5 | // Created by Shawn O'Grady on 12/1/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | /* 9 | This code is a practice C++ interview question from testdome.com 10 | https://www.testdome.com/for-developers/solve-question/7284 11 | "Write a function that checks if a given word is a palindrome. Character case should be ignored." 12 | */ 13 | 14 | #include 15 | #include 16 | #include 17 | #include 18 | 19 | class Palindrome{ 20 | public: 21 | static bool isPalindrome(const std::string &word){ 22 | int stringLength=word.length(); 23 | for(int i=0; i 27 | #include 28 | #include 29 | #include 30 | 31 | class Path 32 | { 33 | public: 34 | Path(std::string path) 35 | { 36 | currentPath = path; 37 | pastPath=path; 38 | } 39 | 40 | std::string getPath() const 41 | { 42 | return currentPath; 43 | } 44 | 45 | void cd(std::string newPath) 46 | { 47 | 48 | int newPathLength=newPath.length(); 49 | 50 | if (newPathLength==1&&(isspace(newPath[0])||newPath[0]=='/')){ 51 | currentPath="/"; 52 | } 53 | else if(newPathLength>0&&newPath[0]!='~'){ 54 | int i=0; 55 | while(i2){ 61 | currentPath.erase(pathLength-2,2); 62 | i=i+3; 63 | } 64 | else{ 65 | //root directory 66 | currentPath="/"; 67 | i=i+3; 68 | } 69 | 70 | } 71 | else if(newPath[i]=='/'){ 72 | //direct pathname 73 | 74 | pastPath=currentPath; 75 | //currentPath.erase(0,pathLength); 76 | currentPath.clear(); 77 | int j=0; 78 | while(j1){ 120 | currentPath=currentPath+"/"+newPath[i]; 121 | } 122 | else{ 123 | currentPath=currentPath+newPath[i]; 124 | } 125 | i=i+2; 126 | } 127 | } 128 | } 129 | else{ 130 | //cd command followed by nothing -> go to root directory 131 | currentPath="/"; 132 | } 133 | 134 | 135 | } 136 | 137 | private: 138 | std::string pastPath; 139 | std::string currentPath; 140 | }; 141 | /* 142 | #ifndef RunTests 143 | int main() 144 | { 145 | Path path("/a/b/c/d"); 146 | //test cases: 147 | //path.cd("../x"); // '/a/b/c/x' -> example test case 148 | //path.cd(".."); // '/a/b/c' -> relative pathname to parent directory 149 | //path.cd("./x"); // '/a/b/c/d/x' -> relative pathname to child directory 150 | //path.cd("x"); // '/a/b/c/d/x' -> relative pathname to child directory 151 | //path.cd("/a/b/c"); // '/a/b/c' -> direct pathname to parent 152 | //path.cd("../../x"); // '/a/b/x' 153 | //path.cd("./x/../../y"); // 'a/b/c/y' 154 | //path.cd("x/../../y"); // 'a/b/c/y' 155 | //path.cd("../../../.."); // '/' 156 | //path.cd("x/.."); // '/a/b/c/d' 157 | //path.cd(""); // '/' 158 | //path.cd("/"); // '/' 159 | //path.cd("~"); // '/' 160 | //path.cd("."); // '/a/b/c/d 161 | //path.cd("/x/y/z"); // '/x/y/z/' -> another direct pathname 162 | //path.cd("x/y/z "); // '/a/b/c/d/x/y/z' 163 | path.cd("/a/b/c/../x"); // '/a/b/x' 164 | 165 | std::cout << path.getPath(); 166 | } 167 | #endif 168 | */ 169 | -------------------------------------------------------------------------------- /sortedSearch.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // sortedSearch.cpp 3 | // sortedSearch 4 | // 5 | // Created by Shawn O'Grady on 12/2/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | 9 | /* 10 | This code is a practice C++ interview question from testdome.com 11 | https://www.testdome.com/for-developers/solve-question/11891 12 | 13 | Problem statement: "Implement function countNumbers that accepts a sorted vector of integers and counts the number of vector elements that are less than the parameter lessThan." 14 | 15 | +Currently function is taking too long to run 16 | -need to find alternative approach 17 | */ 18 | 19 | #include 20 | #include 21 | #include 22 | #include 23 | class SortedSearch 24 | { 25 | public: 26 | static int countNumbers(const std::vector& sortedVector, int lessThan) 27 | { 28 | if(sortedVector.back()<=lessThan){ 29 | //all elements in vector less than given parameter 30 | //int vectorLength=sortedVector.size(); 31 | if(sortedVector.back()==lessThan){ 32 | return sortedVector.size()-1; 33 | } 34 | else{ 35 | return sortedVector.size(); 36 | } 37 | } 38 | else if(sortedVector.front()>=lessThan){ 39 | //no elements less than given parameter 40 | return 0; 41 | } 42 | else{ 43 | 44 | int vectorLength=sortedVector.size(); 45 | int i=1; //# of vector elements less than the given parameter 46 | int j=vectorLength/2; 47 | int k=vectorLength; 48 | while(k>1){ 49 | if(sortedVector[j]lessThan){ 53 | i=j; 54 | j=j/2; 55 | } 56 | else{ 57 | //middle value of vector was equal to less than 58 | i=j; 59 | break; 60 | } 61 | k=k/2; 62 | 63 | } 64 | //i=j-1; 65 | if(sortedVector[j]=lessThan){ 68 | //return j; 69 | i=j; 70 | break; 71 | } 72 | j++; 73 | } 74 | }else if(sortedVector[j]>lessThan){ 75 | while(i=lessThan){ 77 | //return i; 78 | break; 79 | } 80 | i++; 81 | } 82 | 83 | } 84 | else{ 85 | i=j; 86 | } 87 | 88 | 89 | 90 | return i; 91 | 92 | } 93 | } 94 | }; 95 | 96 | -------------------------------------------------------------------------------- /trainComposition.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // trainComposition.cpp 3 | // trainComposition 4 | // 5 | // Created by Shawn O'Grady on 12/5/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | 9 | /* 10 | This code is a practice C++ interview question from testdome.com 11 | https://www.testdome.com/for-developers/solve-question/11908 12 | 13 | Problem statement: A TrainComposition is built by attaching and detaching wagons from the left and the right sides. 14 | -For example, if we start by attaching wagon 7 from the left followed by attaching wagon 13, again from the left, we get a composition of two wagons (13 and 7 from left to right). 15 | -Now the first wagon that can be detached from the right is 7 and the first that can be detached from the left is 13. 16 | -Implement a TrainComposition that models this problem 17 | 18 | + This sounds a lot like a doubly-linked list queue, with the only modification being that you an enqueue+dequeue from either end 19 | 20 | + Runs well with 3/3 tests passed 21 | */ 22 | 23 | #include 24 | #include 25 | #include 26 | //basically just the node for a dll 27 | class TrainNode{ 28 | private: 29 | int value; 30 | TrainNode *leftTrain; 31 | TrainNode *rightTrain; 32 | public: 33 | TrainNode(){leftTrain=rightTrain=NULL;} 34 | TrainNode(int wagonId){ 35 | value=wagonId; 36 | leftTrain=rightTrain=NULL; 37 | } 38 | int getValue(){return value;} 39 | void setLeftTrain(TrainNode *newNode){leftTrain=newNode;} 40 | void setRightTrain(TrainNode *newNode){rightTrain=newNode;} 41 | TrainNode* getRightTrain(){return rightTrain;} 42 | TrainNode* getLeftTrain(){return leftTrain;} 43 | }; 44 | 45 | class TrainComposition 46 | { 47 | private: 48 | TrainNode *leftMost; 49 | TrainNode *rightMost; 50 | public: 51 | TrainComposition(){leftMost=rightMost=NULL;} 52 | void attachWagonFromLeft(int wagonId) 53 | { 54 | TrainNode *tmp=new TrainNode(wagonId); 55 | if(leftMost !=NULL){ 56 | //there are trains in the composition 57 | leftMost->setLeftTrain(tmp); 58 | tmp->setRightTrain(leftMost); 59 | leftMost=tmp; 60 | }else{ 61 | leftMost=tmp; 62 | rightMost=tmp; 63 | } 64 | } 65 | 66 | void attachWagonFromRight(int wagonId) 67 | { 68 | TrainNode *tmp=new TrainNode(wagonId); 69 | if(rightMost !=NULL){ 70 | //there are trains in the composition 71 | rightMost->setRightTrain(tmp); 72 | tmp->setLeftTrain(rightMost); 73 | rightMost=tmp; 74 | 75 | }else{ 76 | leftMost=tmp; 77 | rightMost=tmp; 78 | } 79 | } 80 | 81 | int detachWagonFromLeft() 82 | { 83 | TrainNode *tmp; 84 | if(leftMost!=NULL){ 85 | //there are trins in the composition 86 | tmp=leftMost; 87 | leftMost=leftMost->getRightTrain(); 88 | int tmpValue=tmp->getValue(); 89 | delete tmp; 90 | return tmpValue; 91 | }else{ 92 | return NULL; 93 | } 94 | } 95 | 96 | int detachWagonFromRight() 97 | { 98 | TrainNode *tmp; 99 | if(rightMost!=NULL){ 100 | //there are trins in the composition 101 | tmp=rightMost; 102 | rightMost=rightMost->getLeftTrain(); 103 | int tmpValue=tmp->getValue(); 104 | delete tmp; 105 | return tmpValue; 106 | }else{ 107 | return NULL; 108 | } 109 | } 110 | }; 111 | -------------------------------------------------------------------------------- /twoSum.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // twoSum.cpp 3 | // Two Sum 4 | // 5 | // Created by Shawn O'Grady on 12/1/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | 9 | /* 10 | This code is a practice C++ interview question from testdome.com 11 | https://www.testdome.com/for-developers/solve-question/10283 12 | "Write a function that, given a vector and a target sum, returns zero-based indices of any two distinct elements whose sum is equal to the target sum. 13 | If there are no such elements, the function should return (-1, -1)." 14 | 15 | +Currently this function is taking too long to run 16 | -need to find better alternative to nested loops 17 | */ 18 | 19 | 20 | #include 21 | #include 22 | #include 23 | #include 24 | 25 | 26 | class TwoSum 27 | { 28 | public: 29 | static std::pair findTwoSum(const std::vector& list, int sum) 30 | { 31 | int ListLength=list.size(); 32 | //std::cout<<"list length:"< match=std::pair (-1,-1); 34 | for(int i=0; i (i, j); 39 | match.first=i; 40 | match.second=j; 41 | return match; 42 | } 43 | } 44 | 45 | 46 | } 47 | return match; 48 | } 49 | }; 50 | -------------------------------------------------------------------------------- /userInput.cpp: -------------------------------------------------------------------------------- 1 | // 2 | // userInput.cpp 3 | // User Input 4 | // 5 | // Created by Shawn O'Grady on 12/1/17. 6 | // Copyright © 2017 Shawn O'Grady. All rights reserved. 7 | // 8 | /* 9 | This code is a practice C++ interview question from testdome.com 10 | https://www.testdome.com/for-developers/solve-question/11966 11 | 12 | Problem statement: 13 | +User interface contains two types of user input controls: TextInput, which accepts all characters and NumericInput, which accepts only digits. 14 | +Implement the following methods: 15 | -add on class TextInput - adds the given character to the current value 16 | -getValue on class TextInput - returns the current value 17 | -add on class NumericInput - overrides the base class method so that each non-numeric character is ignored 18 | */ 19 | #include 20 | #include 21 | #include 22 | 23 | class TextInput{ 24 | private: 25 | std::string value; 26 | public: 27 | virtual void add(char c){ 28 | value=value+c; 29 | } 30 | std::string getValue() { 31 | return value; 32 | } 33 | 34 | }; 35 | class NumericInput: public TextInput{ 36 | private: 37 | //std::string value; 38 | public: 39 | void add(char c){ 40 | if(!isalpha(c)){ 41 | //is numeric 42 | return TextInput::add(c); 43 | } 44 | 45 | } 46 | }; 47 | --------------------------------------------------------------------------------