[go: up one dir, main page]

0% found this document useful (0 votes)
2 views3 pages

Dart Basic Array String Questions

The document presents common Dart interview-style questions and their solutions without using prebuilt functions. It includes tasks such as merging sorted arrays, reversing arrays, removing duplicates, summing elements, finding max and min values, reversing strings, counting vowels, checking for palindromes, calculating character frequency, solving the two-sum problem, and merging unsorted arrays. Each solution is provided as a function with clear implementation details.

Uploaded by

priyanshu.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

Dart Basic Array String Questions

The document presents common Dart interview-style questions and their solutions without using prebuilt functions. It includes tasks such as merging sorted arrays, reversing arrays, removing duplicates, summing elements, finding max and min values, reversing strings, counting vowels, checking for palindromes, calculating character frequency, solving the two-sum problem, and merging unsorted arrays. Each solution is provided as a function with clear implementation details.

Uploaded by

priyanshu.kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Common Dart Interview-Style Questions (Without Prebuilt Functions)

==================================================================

1. Merge Two Sorted Arrays (Without Built-ins)


----------------------------------------------
List<int> mergeSortedArrays(List<int> a, List<int> b) {
List<int> result = [];
int i = 0, j = 0;

while (i < a.length && j < b.length) {


if (a[i] < b[j]) {
result.add(a[i]);
i++;
} else {
result.add(b[j]);
j++;
}
}

while (i < a.length) result.add(a[i++]);


while (j < b.length) result.add(b[j++]);

return result;
}

2. Reverse Array (Manual)


--------------------------
List<int> reverseArray(List<int> arr) {
List<int> reversed = [];
for (int i = arr.length - 1; i >= 0; i--) reversed.add(arr[i]);
return reversed;
}

3. Remove Duplicates
---------------------
List<int> removeDuplicates(List<int> arr) {
List<int> unique = [];
for (int i = 0; i < arr.length; i++) {
bool found = false;
for (int j = 0; j < unique.length; j++) {
if (arr[i] == unique[j]) {
found = true;
break;
}
}
if (!found) unique.add(arr[i]);
}
return unique;
}

4. Sum of Elements
-------------------
int sumArray(List<int> arr) {
int sum = 0;
for (int i = 0; i < arr.length; i++) sum += arr[i];
return sum;
}

5. Max & Min


------------
int findMax(List<int> arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) if (arr[i] > max) max = arr[i];
return max;
}

int findMin(List<int> arr) {


int min = arr[0];
for (int i = 1; i < arr.length; i++) if (arr[i] < min) min = arr[i];
return min;
}

6. Reverse String
------------------
String reverseString(String str) {
String reversed = '';
for (int i = str.length - 1; i >= 0; i--) reversed += str[i];
return reversed;
}

7. Count Vowels
----------------
int countVowels(String str) {
int count = 0;
String vowels = "aeiouAEIOU";
for (int i = 0; i < str.length; i++) {
for (int j = 0; j < vowels.length; j++) {
if (str[i] == vowels[j]) {
count++;
break;
}
}
}
return count;
}

8. Check Palindrome
--------------------
bool isPalindrome(String str) {
int start = 0, end = str.length - 1;
while (start < end) {
if (str[start].toLowerCase() != str[end].toLowerCase()) return false;
start++;
end--;
}
return true;
}
9. Char Frequency
------------------
Map<String, int> charFrequency(String str) {
Map<String, int> freq = {};
for (int i = 0; i < str.length; i++) {
String char = str[i];
if (freq.containsKey(char)) freq[char] = freq[char]! + 1;
else freq[char] = 1;
}
return freq;
}

10. Two Sum


------------
List<int> twoSum(List<int> nums, int target) {
for (int i = 0; i < nums.length; i++) {
for (int j = i + 1; j < nums.length; j++) {
if (nums[i] + nums[j] == target) return [i, j];
}
}
return [-1, -1];
}

11. Merge Two Unsorted Arrays


------------------------------
List<int> mergeUnsortedArrays(List<int> a, List<int> b) {
List<int> merged = [];
for (int i = 0; i < a.length; i++) merged.add(a[i]);
for (int i = 0; i < b.length; i++) merged.add(b[i]);
return merged;
}

You might also like