Algorithm

Run Settings
LanguageDart
Language Version
Run Command
void main() { // booo(n){ // for(var i = 0; i < n.length; i++){ // print('boo'); // } // } // booo([1,2,3,4,5]); // space complexity of 0(1) because we only declared var = i and not doing much in the function and we cant control what we put in as in the array // arrayOfNTimes(n){ // List arrayHi = []; // created new array = data structure , takes space // // var = i = variables , takes space // for(var i = 0; i < n; i++){ // arrayHi.add('hi'); // allocation , allocated hi to arrayi , takes space // } // return arrayHi; // } // print(arrayOfNTimes(6)) ; // 0(n) so this function has space complexity of 0(n) // find the ist and nth tweets // compare the items using the dates // const array = // [{'date': 2012, 'tweet': 'hi'}, // {'date': 2013, 'tweet': 'dear'}, // {'date': 2015, 'tweet': 'make'},]; // print('jkbdsdnvgv jk. f'.length); //0(1); //look for a pair of number in the list that give s you the sum of 8 when you add them // check for best possible solution in terms of time and space complexity // using linear time would be more efficient rather than nested loop (quadratic) // List array1 = [1,2,3,9]; // NO // List array2 = [1,2,4,4]; // YES so to check this // int sum = 8; // bool doesTheArrayHasPairSumEqualTo8( int sum, array){ // int low = 0; // int high = array.length-1; // bool thereIsSum = false; // while(low < high){ // if(array[low] + array[high] == sum){ // thereIsSum = true; // } // print (array[low] + array[high] ); // low ++; // high--; // } // return thereIsSum; // } // print(doesTheArrayHasPairSumEqualTo8(8, array2)) ; // bool doesTheArrayHasPairSumEqualTo8( int sum, array){ // Set<int> comp = {}; // array.forEach((value ){ // if(comp.contains(value )){ // print( comp); // return true; // } // comp.add((sum - value).toInt()); // }); // return false; // } // print(doesTheArrayHasPairSumEqualTo8(sum, array2)) ; // Given two array create a function that lets a user know (true/false) // whether these two array contains any common item const array = ['a', 'b','c','x']; const array1 = ['z','y', 'i']; //shoould return false; const array2 = ['a', 'b','c','x']; const array3 = ['z','y', 'a']; //shoould return true; // what are the input and output // if the interviewer said its always array the input // how large can the input get in future // if its always a minimal size then we dont have to worry about big o timecomplexity or space // is our goal to be more efficient as possible, is time complexity or space complexity more important to us // the interviewer might say they just want more efficient function. assuming the array can get very very large // 2 parameters -- no size limit might be what the interviewer says // talk about the easiest solution that comes to mind in this case nested loop //also talk while is no the bbest solution in this case (0(n^2)) quadratic time complexity which we try as muchj as possible in interviews // can we assume always two parameter bool checkIfTheTwoListHasCommonItem(arrayX, arrayY){ // int totalLength = list1.length + list2.length; // List<String> mergedList = arrayX + arrayY; // List<String> firstArray = arrayX; // print(mergedList); bool hasCommon = false; Set<String> comp = {}; // for(var i = 0; i < mergedList.length; i++){ // if(comp.contains(mergedList[i])){ // hasCommon = true; // } // comp.add( mergedList[i]); // } for(var i = 0; i < arrayY.length; i++){ if(arrayX.contains(arrayY[i])){ hasCommon = true; } // comp.add( mergedList[i]); } //Assigning array to object as properties this function is mostly used when trying to improve time complexity // loop throough first array and create object where properties === items in the array Map<String, bool> map = {}; for(var i = 0; i< arrayX.length; i++){ if(map[i] != true){ var item = arrayX[i]; map[item] = true; } } print(map); // loop through the second array and check if the item in the second array exists on created object for(var j = 0; j< arrayY.length; j++){ if(map [arrayY[j]] == true){ hasCommon = true; } } // 0(a) space complexity; return hasCommon; } // print(checkIfTheTwoListHasCommonItem(array2, array3)); //find out if there is a pair that sums up to the number given // sum = 8 //items [] void googleInterview(){ List array1 = [1,2,3,9]; // NO List array2 = [1,2,4,4]; // YES so to check this int sum = 8; // naive way bool checkIfAnyPairSumsUptoTheSum(array, sum){ for(var i = 0; i < array.length -1; i ++ ){ for(var j = 1 + i; j < array.length ; j++ ){ if(array[i] + array[j] == sum) { print('${(array[i], array[j] )}'); return true; } } } return false; } print(checkIfAnyPairSumsUptoTheSum(array2, sum)) ; // Better way bool checkIfAnyPairSumsUptoTheSum1(array, sum){ Set<int> comp = {}; array.forEach((value ){ if(comp.contains(value )){ print( comp); return true; } comp.add((sum - value).toInt()); }); return false; } print(checkIfAnyPairSumsUptoTheSum1(array1, sum)) ; } // Arrays(); }
void Arrays(){ const strings = ['a','b','c',d]; //4*4 = 16 bytes of storage strings[2]; }
Editor Settings
Theme
Key bindings
Full width
Lines