0% found this document useful (0 votes)
7 views1 page

Python Coding Test Questions

The document contains coding practice questions and solutions in Python and JavaScript, covering various topics such as array traversal, finding minimum and maximum values, reversing arrays, checking for palindromes, counting vowels, linear and binary search, and bubble sort. Each question is accompanied by sample code snippets in both programming languages. This serves as a resource for practicing fundamental coding concepts.

Uploaded by

Ketan Sutar
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)
7 views1 page

Python Coding Test Questions

The document contains coding practice questions and solutions in Python and JavaScript, covering various topics such as array traversal, finding minimum and maximum values, reversing arrays, checking for palindromes, counting vowels, linear and binary search, and bubble sort. Each question is accompanied by sample code snippets in both programming languages. This serves as a resource for practicing fundamental coding concepts.

Uploaded by

Ketan Sutar
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

Coding Practice Questions (Python & JavaScript)

1. Array Traversal
Q: Write a program to traverse an array and print all elements.
Python:
arr = [10, 20, 30, 40, 50] for i in arr: print(i) JavaScript:
let arr = [10, 20, 30, 40, 50]; [Link](e => [Link](e));
2. Find Minimum and Maximum in Array
Q: Write a program to find min and max in an array.
Python:
arr = [12, 45, 2, 19, 8] print("Min:", min(arr)) print("Max:", max(arr)) JavaScript:
let arr = [12, 45, 2, 19, 8]; [Link]("Min:", [Link](...arr)); [Link]("Max:",
[Link](...arr));
3. Reverse an Array
Python:
arr = [1, 2, 3, 4, 5] print(arr[::-1]) JavaScript:
let arr = [1, 2, 3, 4, 5]; [Link]([Link]());
4. Palindrome String
Q: Check if a string is palindrome.
Python:
s = "madam" print(s == s[::-1]) JavaScript:
let s = "madam"; [Link](s === [Link]("").reverse().join(""));
5. Count Vowels in String
Python:
s = "hello world" count = sum(1 for ch in s if ch in "aeiouAEIOU") print("Vowels:", count) JavaScript:
let s = "hello world"; let count = [Link]("").filter(ch => "aeiouAEIOU".includes(ch)).length;
[Link]("Vowels:", count);
6. Linear Search
Python:
arr = [10, 20, 30, 40, 50] x = 30 found = -1 for i in range(len(arr)): if arr[i] == x: found = i break
print("Found at index:", found) JavaScript:
let arr = [10, 20, 30, 40, 50]; let x = 30; let idx = [Link](x); [Link]("Found at index:", idx);
7. Binary Search (sorted array)
Python:
def binary_search(arr, x): low, high = 0, len(arr) - 1 while low <= high: mid = (low + high) // 2 if
arr[mid] == x: return mid elif arr[mid] < x: low = mid + 1 else: high = mid - 1 return -1
print(binary_search([10, 20, 30, 40, 50], 30)) JavaScript:
function binarySearch(arr, x) { let low = 0, high = [Link] - 1; while (low <= high) { let mid =
[Link]((low + high) / 2); if (arr[mid] === x) return mid; else if (arr[mid] < x) low = mid + 1; else
high = mid - 1; } return -1; } [Link](binarySearch([10,20,30,40,50], 30));
8. Bubble Sort
Python:
arr = [64, 25, 12, 22, 11] n = len(arr) for i in range(n): for j in range(0, n-i-1): if arr[j] > arr[j+1]: arr[j],
arr[j+1] = arr[j+1], arr[j] print(arr) JavaScript:
let arr = [64, 25, 12, 22, 11]; for (let i = 0; i < [Link]; i++) { for (let j = 0; j < [Link] - i - 1; j++) { if
(arr[j] > arr[j+1]) { [arr[j], arr[j+1]] = [arr[j+1], arr[j]]; } } } [Link](arr);

Common questions

Powered by AI

The iterative approach for binary search, seen in the code outlined in the sources, uses a loop to navigate a sorted array, which efficiently maintains control over the call stack, ensuring a constant space complexity O(1). In contrast, a recursive binary search, though elegant and straightforward, increases call stack size with each recursive call, resulting in a space complexity of O(log n) due to stack frame creation. Both have the same time complexity O(log n), but iterative methods are often preferred in constrained environments to prevent stack overflow .

Both Python and JavaScript provide straightforward ways to find minimum and maximum values in an array, using built-in functions. In Python, the `min()` and `max()` functions directly compute the minimum and maximum. JavaScript uses `Math.min(...arr)` and `Math.max(...arr)` with the spread operator to achieve the same. Both methods iterate over the array one time which results in a time complexity of O(n).

In Python, reversing an array can be done using slicing with the syntax `arr[::-1]`, which creates a new reversed array in a single step, providing an efficient and concise method. In JavaScript, using `arr.reverse()` reverses the array in place and alters the original array. Both methods have a time complexity of O(n), where n is the number of elements, but Python creates a copy of the array which can use additional memory .

Linear search is a straightforward method of searching for an element that involves checking every element in the array until the desired one is found, making it useful for unsorted arrays or when the cost of maintaining sorted data isn't justified. However, it has a time complexity of O(n), where n is the number of elements. Binary search, on the other hand, requires a sorted array and has a time complexity of O(log n), making it much more efficient for larger datasets. Therefore, the choice of algorithm heavily depends on the size of the data and whether it is or can be sorted .

Understanding data traversal techniques, such as iteration, recursion, and depth-first or breadth-first search, is crucial as they directly affect algorithm efficiency and performance. Efficient traversal minimizes time complexity and optimizes resource usage, directly impacting application performance in handling large and complex data structures like graphs, trees, and arrays. By selecting appropriate traversal strategies, developers can efficiently manage memory, computational load, and responsiveness, crucial for applications requiring real-time performance and robust data processing .

The `forEach` method in JavaScript provides a convenient way to iterate over array elements, but it has some limitations, such as being unable to terminate early once a condition is met (unlike a for-loop which can use break statements). `forEach` also does not work with asynchronous code without additional handling, as it does not wait for promises. It creates a performance overhead by calling a higher order function for each element, potentially impacting performance for very large datasets. This underlines the importance of choosing the right iteration method based on the problem's requirements .

Both Python and JavaScript utilize loops and conditional logic to count vowels in a string. In Python, a generator expression with `sum` function iteratively counts vowels efficiently. JavaScript uses a filter method combined with `split` to achieve the same goal. Python's approach leverages its strong emphasis on readable, expressive syntax, while JavaScript's functional style highlights its array-processing capabilities. This showcases Python's readability against JavaScript's in-line functional possibilities .

Bubble sort compares adjacent pairs of elements and swaps them if they are in the wrong order, repeatedly passing over the array until it is sorted. The steps include iteratively traversing through the list, comparing each element with the next, and swapping them if needed, with the largest unsorted element 'bubbling' to the top during each pass. This process continues until no more swaps are needed. Despite its simplicity, bubble sort is inefficient for large datasets with a time complexity of O(n^2), making it suitable primarily for educational purposes or small datasets with few elements .

Python's `min` and `max` functions are directly applied on iterables like lists to find the smallest or largest element, leveraging Python's internal iteration capabilities. Meanwhile, JavaScript's `Math.min` and `Math.max` require the spread syntax to handle arrays because they expect a list of arguments rather than an iterable. This reveals Python's orientation towards iterable operations, while JavaScript traditionally anticipates discrete function arguments, yet modern enhancements like the spread operator mitigate this .

Palindrome verification is common in programming due to its conceptual simplicity yet useful application in areas like data validation, cryptography, and bioinformatics. It introduces fundamental concepts of string manipulation and algorithm efficiency. Real-world applications include checking data integrity, generating checksums, and analyzing nucleotide sequences in DNA (which can exhibit palindromic properties due to structural reversibility).

You might also like