Read N characters given read7

Problem Using a read7() method that returns 7 characters from a file, implement readN(n) which reads n characters. For example, given a file with the content “Hello world”, three read7() returns “Hello w”, “orld” and then “”. Solution Method 1 - Using Iteration Here are the steps: Buffer Management: Maintain a buffer to store characters read by read7() that weren’t consumed by readN(n) calls. Reading n Characters: Continuously read using read7() until we have at least n characters in total. If read7() returns fewer than 7 characters, we’ve likely reached the end of the file. Serve the Required Characters: Serve characters from the buffer to fulfill the readN(n) request. Code Java public class FileReader { private String content; private int index; public FileReader(String content) { this.content = content; this.index = 0; } public String read7() { if (index >= content.length()) { return ""; } int end = Math.min(index + 7, content.length()); String result = content.substring(index, end); index += 7; return result; } } public class Solution { private FileReader fileReader; private Queue<Character> buffer; public Solution(FileReader fileReader) { this.fileReader = fileReader; this.buffer = new LinkedList<>(); } public String readN(int n) { StringBuilder result = new StringBuilder(); while (result.length() < n) { // If buffer is empty, read more characters using read7() if (buffer.isEmpty()) { String readData = fileReader.read7(); if (readData.isEmpty()) { break; } for (char c : readData.toCharArray()) { buffer.offer(c); } } // Add buffered characters to the result while (!buffer.isEmpty() && result.length() < n) { result.append(buffer.poll()); } } return result.toString(); } } ...

Merge Nodes in Between Zeros Problem

Problem You are given the head of a linked list, which contains a series of integers separated by 0’s. The beginning and end of the linked list will have Node.val == 0. For every two consecutive 0’s, merge all the nodes lying in between them into a single node whose value is the sum of all the merged nodes. The modified list should not contain any 0’s. Return the head of the modified linked list. ...

Nth Number with sum of digits as 10

Problem A number is considered perfect if its digits sum up to exactly 10. Given a positive integer n, return the n-th perfect number. Examples Example 1: Input: n = 1 Output: 19 Example 2: Input: n = 2 Output: 28 Solution Method 1 - Brute Force Keep on generating the numbers with sum of digits as 10, and when count is n, return the number. ...

Construct Binary Tree from Inorder and Postorder Traversal

Problem Given two integer arrays inorder and postorder where inorder is the inorder traversal of a binary tree and postorder is the postorder traversal of the same tree, construct and return the binary tree. Example Example 1: 3 / \ 9 20 / \ 15 7 ...

Find Median from Data Stream

Problem Given that integers are read from a data stream. Find median of elements read so for in efficient way. OR Numbers are randomly generated and passed to a method. Write a program to find and maintain the median value as new values are generated. OR You are given a stream of numbers which can be positive or negative. You are required to provide an operation FIND MEDIAN..which when invoked should be able return the median of the numbers in stream(in say O(1) time) ...

First Unique Character in a Stream of Characters

Find First Unique Character From a Stream of Characters Problem Given a string A denoting a stream of lowercase alphabets. You have to make new string B. B is formed such that we have to find first non-repeating character each time a character is inserted to the stream and append it at the end to B. If no non-repeating character is found then append ’#’ at the end of B. ...

Square root of an integer Problem

Problem Implement int sqrt(int x). OR Compute and return the square root of x. OR If x is not a perfect square, return floor(sqrt(x)) OR floor(√n) OR Given a non-negative integer x, compute and return the square root of x. Since the return type is an integer, the decimal digits are truncated, and only the integer part of the result is returned. ...

Word Break 2 - Construct a sentence

Problem Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order. Note that the same word in the dictionary may be reused multiple times in the segmentation. Examples Example 1: Input: s = "catsanddog", wordDict = ["cat","cats","and","sand","dog"] Output: ["cats and dog","cat sand dog"] ...

Longest Increasing Subsequence LIS Problem

Problem Given an unsorted array of integers, find the length of longest increasing subsequence. Examples Example 1: Input: A = [10,9,2,5,3,7,101,18] Output: 4 Explanation: The longest increasing subsequence is [2,3,7,101], therefore the length is 4. ...

Lowest Common Ancestor of a Binary Tree

Problem Given a binary tree, find the lowest common ancestor (LCA) of two given nodes in the tree. Definition Lowest Common Ancestor Definition Examples Example 1: graph TD; 6; 6 --- 2; 6 --- 8; 2 --- 0; 2 --- 4; 8 --- 7; 8 --- 9; 4 --- 3; 4 --- 5; style 2 fill:#FF9933 style 8 fill:#FF9933 style 6 fill:#3399FF ...

This site uses cookies to improve your experience on our website. By using and continuing to navigate this website, you accept this. Privacy Policy